Elephant and Bananas

Problem There are 2 cities A and B, 1000 Kms apart. We have 3000 bananas in city A and a elephant, which can carry max 1000 bananas at any given time. The elephant needs to eat a banana every 1 Km. How many maximum number of bananas can be transferred to city B? Note: The elephant cannot go without bananas. Generalized Question: There are 2 cities A and B, ‘D’ Km apart. [Read More]

Why does mirror lies sometimes?

Problem Imagine you are standing in front of a mirror, facing it. Raise your left hand. Raise your right hand. Look at your reflection. When you raise your left hand your reflection raises what appears to be his right hand. But when you tilt your head up, your reflection does too, and does not appear to tilt his/her head down. Why is it that the mirror appears to reverse left and right, but not up and down? [Read More]

Average salary of n people in the room

Question: How can n people know the average of their salaries without disclosing their own salaries to each other? Solution: Let’s say salaries of n people (P1, P2, P3…..Pn) are S1, S2, S3…..Sn respectively. To know the average of the salary i.e. (S1 + S2 + S3 + ….. + Sn) / n, they will follow the following steps - P1 adds a random amount, say R1 to his own salary and gives that to P2 (P2 won’t be able to know P1’s salary as he has added a random amount known to him only). [Read More]

Point inside a triangle or NOT

Problem Given three corner points of a triangle, and one more point P. Write a function to check whether P lies within the triangle or not. Example For example, consider the following program, the function should return true for P(10, 15) and false for P’(30, 15) B(10,30) / \\ / \\ / \\ / P \\ P' / \\ A(0,0) ----------- C(20,0) Solution Method 1 - Area of triangles made by point and other 2 vertices of triangle [Read More]

Triangle Formation

Problem Write a function that takes three integers corresponding to the lengths of sides and returns what kind of triangle can be made out of those 3 sides. (Equilateral, etc) You must also handle the case where no triangle may be formed from those 3 lengths. Solution We have to do following if(2 sides greater than 1){ if(a==b && b==c) // all sides equal "Equilateral triangle\\n"; else if(a==b || a==c || b==c) // at least 2 sides equal "Isosceles triangle\\n"; else // no sides equal "Scalene triangle\\n"; }else "Not a triangle"; Thanks. [Read More]

Find the average of n numbers

Problem Given a set of n numbers (data type long). Write an algorithm to get the average. Constraints It should work if one of the contributing number is the maximum possible number of type long. Casting it to other data types is not allowed. Solution We can’t use addition for getting the average. If we do, first constraint might not get incorporated as we may overflow. . [Read More]

Fill a magic square of size n, n being odd

Problem Write an algorithm to fill a magic square of size 3. You are given a position to start with (location of 1 on any edge). Background Magic square has following properties - No number is repeated. All rows, columns and diagonals give equal sum The magic constant of a normal magic square depends only on n and has the following value: M = n(n^2+1)/2. eg. for 3, M = 3 * (9+1)/2 = 15. [Read More]

CTRL+A, CTRL+C, CTRL+V

Code given by does not give desired output.. pleas…

Anonymous - May 3, 2014

Code given by does not give desired output.. please check the code given below working fine.

int MaxCopy(int n){
int *table=(int *)malloc(sizeof(int)*(n+1));
memset(table,0,sizeof(int)*(n+1));

for(int i=0;i<=n;i++){
table[i]=i;
}
for(int i=0;i<=n;i++){
for(int j=i+4;j<=n;j++){
table[j]=max(table[j],table[i]*(j-i-2));
}
}
int res=table[n];
free(table);
return res;
}

Thanks man for fixing it. I will update it soon.

CTRL+A, CTRL+C, CTRL+V

Problem Imagine you have a special keyboard with the following keys:  A Ctrl+A Ctrl+C Ctrl+V where CTRL+A, CTRL+C, CTRL+V each acts as one function key for “Select All”, “Copy”, and “Paste” operations respectively. If you can only press the keyboard for N times (with the above four keys), please write a program to produce maximum numbers of A. If possible, please also print out the sequence of keys. That is to say, the input parameter is N (No. [Read More]

Guess the solution of selecting 4 balls from set of 4 different colors

Problem A solution consists of four balls from a set of four different colors. The user tries to guess the solution.If they guess the right color for the right spot, record it as being in the correct ‘Location’. If it’s the right color, but the wrong spot, record it as a correct ‘Color’. For example: if the solution is ‘BGRR’ and the user guesses ‘RGYY’ they have 1 ‘Location’ and 1 ‘Color’. [Read More]