-
Notifications
You must be signed in to change notification settings - Fork 0
/
ArrayInversionCount.java
37 lines (27 loc) · 1.02 KB
/
ArrayInversionCount.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
import java.util.*;
class Solution {
private static final int LIMIT_ARRAY_LENGTH = 1000000000;
public int solution(int[] A) {
if (checkExceedLimit(A.length)) return -1;
return getArrayLengthAndInversionElements(A);
}
private boolean checkExceedLimit(int arrayLength) {
return arrayLength > LIMIT_ARRAY_LENGTH;
}
private int getArrayLengthAndInversionElements(int[] A) {
final int arrayLength = A.length;
return iterateArrayAndGetInversionCount(arrayLength, A);
}
private int iterateArrayAndGetInversionCount(int arrayLength, int[] array) {
int inversionCount = 0;
for (int i=0; i<arrayLength; i++) {
for (int j=i+1; j<arrayLength; j++) {
if (isCurrentElementiggerThanNext(array[i], array[j])) inversionCount++;
}
}
return inversionCount;
}
private boolean isCurrentElementiggerThanNext(int currentElement, int nextElement) {
return currentElement > nextElement;
}
}