-
Notifications
You must be signed in to change notification settings - Fork 0
/
Solution3.java
42 lines (34 loc) · 1.34 KB
/
Solution3.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
public class Solution3 {
public int solution(int[] ranks) {
if(ranks.length == 0)
return 0;
int numberOfSoldiersWithSameSuperior = 0;
QuickSort q = new QuickSort();
q.sort(ranks, 0, ranks.length-1);
Integer max = ranks[ranks.length-1];
Integer previousRank = -1;
Integer previousRankCount = 0;
for(int i=0; i<ranks.length; i++) {
Integer currentRank = ranks[i];
// Nothing to be done for First Eelement
if(i == 0) {
previousRank = currentRank;
previousRankCount++;
}else if(currentRank != previousRank) {
// Rank has been changed
// Check if this current Rank is a superior for the previous Rank.
if(currentRank == previousRank+1) { // superior Exists
// If current Rank is a superior of the previous rank then add to the totalNumber
numberOfSoldiersWithSameSuperior += previousRankCount;
}
// Change the previousRank to CurrentRank
previousRank = currentRank;
// Set the previous Rank count to 1
previousRankCount = 1;
}else {
previousRankCount++;
}
}
return numberOfSoldiersWithSameSuperior;
}
}