itnilesh
BAN USER- -1of 1 vote
AnswersForm Tree from following data
- itnilesh in United States
I have data like this
6 has parent SET 5,4
5 has parent SET 3,4,1,2
4 has parent SET 2,1,4
3 has parent SET 2,1
I need to create tree like
1 --> 2 --> 3 --> 4 --> 5 --> 6
OR
2--> 1--> 3--> 4-- >5 --> 6
Because there is no enough info about 1-->2 or 2--> 1| Report Duplicate | Flag | PURGE
Symantec Principal Software Engineer Algorithm
public static void main(String[] args) {
int[] testArr = new int[] { 1,1, 9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9
};
int maxFreq = 0;
int currentFreq = 0;
int max = testArr[0];
for (int i = 1; i <= testArr.length - 1; i++) {
currentFreq++;
int prev = testArr[i-1];
int next = testArr[i];
if (prev != next || (i == testArr.length - 1)) {
if (maxFreq <= currentFreq) {
max = testArr[i-1];
maxFreq = currentFreq;
}
currentFreq = 0;
}
}
System.out.println("Max freq " + maxFreq + " Number " + max);
}
complexity is of O(n)
- itnilesh January 15, 2014