Round 1: Technical Face 2 Face.
- Write a program to find the range of majority elements in array(non-descending)?
- Write program to print all the permutation of String? Example Input : AB, Output : {$, “A”, “B”, “AB”};
- Write program to transform a tree from give tree, such that each node will have the sum of child nodes plus itself.(Write recursive function)
To best of me i have given answer to all the three programs(little doubtful with 2nd one).
No Selected. 🙁
For first question my approach was:
public class MajorityRange {
public static void main(String args[])
{
int number[] = { 1 , 3 , 3 , 3 , 3 , 3 , 4 };
int size = number.length;
int start = 0 ;
int end = size - 1 ;
int count = 1 ;
for ( int i = 1 ; i < size; i++) {
if (number[i] != number[i - 1 ]) {
if (count > size / 2 ) {
end = i - 1 ;
}
else {
start = i;
}
}
else {
count++;
}
}
System.out.println( "Start = " + start + "End = " + end);
}
} |
Explanation: Solved in O(n) time complexity. In order to improve the complexity we can use divide and conquer strategy(Modified version of Jump Search or interpolation Search)
Article Tags :
Recommended Articles