Close

## Type casting in C++ – Part 1

Type casting frequently arises in real life programming. This post concentrates on type conversion of fundamental data types. Type casting is the process of converting from one data type to another. In C++, type casting is automatic in some situations. These are called implicit conversions. For example char c = ‘A’; int ch_code = c;…

## Google codejam 2014 – Qualification Round Problem – Magic trick

The following problem is from the Google code jam 2014 qualification round.  Head-over there to read the complete problem description.   The problem description is as follows. A magician starts by arranging 16 cards in a square grid: 4 rows of cards, with 4 cards in each row. Each card has a different number from…

## C++ STL Algorithms – Sort – Part-2

In the last post, the basic use of STL sort() method is explained. In this post, I will discuss some more options available with this method. We have seen in the previous post that sort takes the beginning and ending of the array as arguments. sort( array.begin(), array.end() ); In addition to those parameters, it…

## C++ STL Algorithms – Sort- Part-1

Sorting is one of the most widely used algorithmic primitive in programming.  C++ Standard Template Library (STL) provides an efficient implementation of the sort algorithm. It is always better to use this algorithm instead of writing our own implementation because of the following benefits. It’s performance would surely be better than your own implementation. It’s…

## Size of an empty object in C++

What is the size of an empty object in C++? This is one of the most frequently asked questions on forums. For example consider the following code snippet. class A { }; A aOb; cout << sizeof(aOb); The size of an empty object is not zero to ensure that the addresses of two different objects…

## Generating a pascal triangle

In this post, we will write a program to generate a pascal triangle of given height. For example, Pascal triangle of height 5 is shown below 1 1 1 1 2 1 1 3 3 1 1 4 6 4 1 The idea behind Pascal triangle is that each element in a row is the…

## Removing all instances of a number in an array

Given an array of arbitrary numbers and a given value, how do we write a program to delete all instances of the given value? For example, if the input array is [23, 9, 18, 9, 6, 47, 3, 6] and the element to be deleted is 9. The result array should be [23, 18, 6,…

## Finding an element in a circularly sorted array

Given a circularly sorted array, how to search for an element efficiently? For example let us consider the array [12, 15, 6, 8, 10], the element 8 is found at index 3. Since the array is sorted but rotated, we can expect it to be solved using the binary search technique. We can design an…

## Finding the magic index of the array

Given a sorted array of distinct values, how do you find an index i such that array[i] = i? Let us call it a magic index.For example if the given array is [-23, -4, 2, 19, 56]. Element 2 is found at array[2]. So the output is 2.If there no such element in the array…

## Finding the number of pairs in array with given difference

Given an array of N distinct values, how to find the number of pairs with a difference of K? For example, given an array [3, 1, 4, 2, 5] and K= 2, there are 3 pairs with a difference of 2. Namely {1,3}, {2,4}, {3,5}.  One obvious solution could be to examine each possible pair…