Fresh_man
BAN USERHere is the code i came up with
static void findNextBiggerNumber(int num) {
int a[] = getAsArray(num);
int length = a.length - 1;
// use sentinel if your number contains 0
int temp[] = new int[length + 1];
int counter = 0;
for (int index = length; index >= 0; index--) {
if (index - 1 >= 0 && a[index - 1] > a[index]) {
temp[counter++] = a[index];
} else {
if (index > 0) {
temp[counter++] = a[index];
BubbleSort.sort(temp);
swapDigit(a, temp, index);
// put the numbers back into original array
for (int j : temp) {
// Need to check, coz int values are 0 by default
// insert sentinel if you want to have 0 in your number
if (j > 0) {
a[index++] = j;
}
}
}
break;
}
}
print(a);
}
private static void swapDigit(int[] a, int[] temp, int i) {
int digitToBeSwapped = a[i - 1];
for (int smallest = 0; smallest < temp.length; smallest++) {
if (temp[smallest] > digitToBeSwapped) {
a[i - 1] = temp[smallest];
temp[smallest] = digitToBeSwapped;
break;
}
}
}
Difference of nodes' values at odd and even level is nothing but root's value-(sum of root immediate children nodes' values)
private static int findDiff(Node node) {
if (node == null)
return 0;
int leftValue = findDiff(node.left);
int rightValue = findDiff(node.right);
return node.value - (leftValue + rightValue);
}
Thanks.
- Fresh_man December 08, 2012
Here is the code for java programmers
- Fresh_man April 13, 2014