Ashish19121
BAN USER#define TRUE 1
#define FALSE 0
int is_palindrome(int x)
{
int reverse = 0;
int result;
int y = abs(x);
while (y != 0) {
reverse = reverse << 1;
if ((y & 1))
reverse = reverse | 1;
y = y >> 1;
}
result = reverse & abs(x);
if (x < 0) result = 0 - result;
return (x == result) ? TRUE : FALSE;
}
@Gupt: I think index contains the value of last index in the array which is used in the if condition to make sure size will not cross index value.
- Ashish19121 March 10, 2013Same can be achieved using BFS.
- Ashish19121 March 09, 2013Place 2 pointers at the end of each array and then compare the elements. The bigger one moves at the end of second array and then decrement the pointer which is pointing to big element. Repeat this step till you finish up with all the elements of the array.
Ex: first array 1 2 3 4 5 (n = 5)
Second array 3 5 6 7 8 9 10 (n = 5, m = 2) (As per the question second array size is big enough to hold elements from the first array. Hence size of it is 12)
Now have one pointer to 5 of first array, another pointer to 10.
Compare 5 and 10, move 10 to end of second array (ofcourse have to maintain pointer for this)
Decrement pointer to point to 9 in the second array.
Repeat this step until all the elements are done.
Here when we encounter same element twice while comparing, place that number twice and then decrement pointers of both array to point to prev element
Hope I am clear in explaining the algorithm
I would say its best to use data structure like this
struct t {
int n;
struct t *kids;
struct t *siblings;
}
Using above structure, while constructing the tree assume kids are always on left of a node and siblings on the right side of a node. This would look like a binary tree.
Hence at the end you can use DFS to check whether cycle is there or not.
Correct me if I am wrong.
Toplogical Sorting will be good solution.
- Ashish19121 March 07, 2013that would not make sense, so at the end I would say to check the length of original string with compressed string and if compressed string length is larger than original, let the program return the original string
- Ashish19121 March 07, 2013string compress(string s)
{
string result;
int count = 1;
for (int i = 0; i < s.length(); i++) {
if (s[i] == s[i + 1]) {
count++;
continue;
} else {
result = result + s[i];
char n[20];
sprintf(n, "%d", count);
result = result + n;
count = 1;
}
}
if (result.length() > s.length())
return s;
else
return result;
}
- Ashish19121 March 07, 2013Assuming the matrix is n*n
void rotate(int *mat[], int n)
{
int layer = 0, first = 0, last = 0, temp = 0, i = 0;
for (layer = 0; layer < n/2; layer++)
{
first = layer;
last = n - 1 - first;
for (i = first; i < last; i++)
{
temp = mat[first][i];
mat[first][i] = mat[last - i][first];
mat[last - i][first] = mat[last][last - i];
mat[last][last - i] = mat[i][last];
mat[i][last] = temp;
}
}
}
Hey Kalyan ... once you have inorder traversal you can find the kth largest element in O(1) as the elements will be in increasing order. So i think without stack jus writing elements into an array while traversing and then w.r.t index required we will know the kth max element in O(1).
- Ashish19121 March 06, 2013int insert(int m, int n, int i, int j)
{
int temp = j - i + 1;
int temp1 = (1 << temp) - 1;
int mask = ~(temp1 << i);
n = n & mask;
n = n | (m << i);
return n;
}
unsigned int reverse_binary(int n)
{
unsigned int temp = n, result = 0;
while (temp != 0) {
if(temp & 1)
result = result | 1;
result <<= 1;
temp >>= 1;
}
result >>= 1;
return result;
}
Also one more condition which I forgot to put is if n is negative then return 0
- Ashish19121 January 23, 2013int count(int n)
{
if (n == 0) return 1;
else if (n == 1) return 1;
else if (n == 2) return 2;
else return count(n-3) + count(n-2) + count(n-1);
}
Since both the given values are present in BST, just finding out depth(root, val1)+depth(root,val2) is enough right. This will give us the answer.
- Ashish19121 March 19, 2013May be for validation sake we can find out if the given node values are actually present or not.