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]
Point inside a rectangle or not
Problem Given a 2D point and a rectangle, determine if the point is inside the rectangle.
Solution Method 1 - Calculate the area of triangles with 2 vertices of rectangle and point
P(x,y), and rectangle A(x1,y1),B(x2,y2),C(x3,y3),D(x4,y4)
Calculate the sum of areas of △APD,△DPC,△CPB,△PBA.
If this sum is greater than the area of the rectangle, then P(x,y) is outside the rectangle. Else if this sum is equal to the area of the rectangle (observe that this sum can not be less than the later), if area of any of the triangle is 0, then P(x,y) is on the rectangle (in fact on that line corresponding to the triangle of area=0).
[Read More]
Given a number x, less than 100. How will you generate true with probability x/100.
Problem Given a number x, less than 100. How will you generate true with probability x/100. So if x = 65, how will you generate true with probability 65/100. You can represent true by 1 and false by 0.
Solution We can make use of rand() function to generate a number less than 100 by taking rand()%100. If the number is less than x, you can output true, otherwise you output false.
[Read More]
Given a number 123456789, two opearators + and *, value k , find all the such expressions that evaluates to the given value k
Problem Given a number 123456789 and two opearators + and *. You can use this two operators as many times u want. But you cant change the sequence of the number given there. The evaluated value is 2097.
e.g. 1+2+345*6+7+8+9=2097
You have to find all the such expressions that evaluates and value is equal to the given value. You can use concatenation of numbers like 345 is concatenated there.
[Read More]