@@ -21,7 +21,7 @@ public class MajorityElementInSortedArray {
21
21
*/
22
22
public static boolean isMajorityElement (int [] a , int n ) {
23
23
int l = a .length ;
24
- int startIndex = getIndexOf (a , 0 , l - 1 , n );
24
+ int startIndex = getFirstIndexOf (a , n , 0 , l - 1 );
25
25
26
26
// element not found
27
27
if (startIndex == -1 ) return false ;
@@ -43,7 +43,7 @@ public static boolean isMajorityElement(int[] a, int n) {
43
43
* @param n
44
44
* @return
45
45
*/
46
- public static int getIndexOf (int [] a , int low , int high , int n ) {
46
+ public static int getFirstIndexOf (int [] a , int n , int low , int high ) {
47
47
if (low <= high ) {
48
48
int mid = (low + high ) / 2 ;
49
49
/**
@@ -53,12 +53,12 @@ public static int getIndexOf(int[] a, int low, int high, int n) {
53
53
* (i) mid == 0 and a[mid] == n
54
54
* (ii) n > a[mid-1] and a[mid] == n
55
55
*/
56
- if ((mid == 0 || n > a [mid - 1 ]) && ( a [ mid ] == n )) {
56
+ if (a [ mid ] == n && (mid == 0 || n > a [mid - 1 ])) {
57
57
return mid ;
58
58
} else if (n <= a [mid ]) {
59
- return getIndexOf (a , low , mid - 1 , n );
59
+ return getFirstIndexOf (a , n , low , mid - 1 );
60
60
} else {
61
- return getIndexOf (a , mid + 1 , high , n );
61
+ return getFirstIndexOf (a , n , mid + 1 , high );
62
62
}
63
63
}
64
64
return -1 ;
0 commit comments