neeraj.fullwithattitude
BAN USERN is the number given.
int stack[N];
sum(int n, int index)
{
if(n==0)
{
print stack;//use a while loop till index -1
return;
}
else
{
for all i=1 to ceil((float)n/2))
{
stack[index] = i;
print stack and n-i; // all elements of stack and n-i
sum(n-i, index+1);
}
}
}
initially call sum(N,0)
If u want me 2 write the code, let me know
- neeraj.fullwithattitude September 17, 2011Can be easily solved using recursion.
for sum(30)
print(1,29);
sum(30) = sum(29) + {1}<-My Stack
print(1,1,28);
sum(29) = sum(28) + {1,1}
You will be given with a dictionary. Find the word in the dictionary closest to the wrong word using some edit distance algorithm like levenshtein distance.
- neeraj.fullwithattitude September 16, 2011Node * mirror(Node *n)
{
Node *temp = n;
while(n!=NULL && n->value!=value && temp!=NULL)
{
if(n->value < value)
{
n = n->right;
temp = temp->left;
}
else
{
n = n->left;
temp = temp->right;
}
}
if(temp!=NULL && n!=NULL)
return temp;
else
return NULL;
}
@vicky, iterative solution is better than recursive
If you lyk the solution, plz. let me know :)
- neeraj.fullwithattitude September 15, 2011void modifiedMerge(int A[], int B[], int size1, int size2)
{
int i = 0;
int j= 0;
while(i<size1 && j<size2)
{
if(A[i]==B[j]) printf("%d ",A[i]);//you can store it in array
else if(A[i] < B[j]) i++;
else j++;
}
}
Guys you can get a linear time algorithm by using modified merge. Using modified merge, find the common elements of first two arrays and store the result in fourth array. Again repeat the same procedure on third and fourth array. You get the answer.
By modified merge, I mean while merging select the common elements.
Here k = 3, so it will do.
- neeraj.fullwithattitude September 15, 2011->Sort the string using any sorting algorithm
-> To generate permutations in lexicographical order, use the code below
char *str;//Sorted String
char *temp;//char array of size 1 greater than that of str
void permutations(int index)
{
if(index == strlen(str)) then print temp array;
int i = 0;
while(i<strlen(str))
{
if(str[i]<0){ i++; continue; }
temp[index] = str[i];
str[i] *= -1;
permutation(index + 1);
str *= -1;
i++;
}
}
Here you will be given with a dictionary. Whenever you will
type a wrong word, the word processor will suggest you with
words from dictionary closest to the wrong word.
For finding the closest word, use 'Levenshtein Distance' algorithm.
A similar problem is here :-
facebook.com/careers/puzzles.php?puzzle_id=17
By overflow, do you mean that all the words can not be brought in memory?
- neeraj.fullwithattitude September 13, 2011
Use Hash Table. Start from index 0 and hash the elements one by one.
- neeraj.fullwithattitude September 18, 2011When ever u change the count of a element, check whether the new count is greater than n/2 or not. If yes, then this is your majority element.
Time Complexity O(n).