Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 8b7305a

Browse files
author
Ram swaroop
committed
majority element in sorted array : refactoring
1 parent b8af09c commit 8b7305a

File tree

1 file changed

+5
-5
lines changed

1 file changed

+5
-5
lines changed

src/me/ramswaroop/arrays/MajorityElementInSortedArray.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public class MajorityElementInSortedArray {
2121
*/
2222
public static boolean isMajorityElement(int[] a, int n) {
2323
int l = a.length;
24-
int startIndex = getIndexOf(a, 0, l - 1, n);
24+
int startIndex = getFirstIndexOf(a, n, 0, l - 1);
2525

2626
// element not found
2727
if (startIndex == -1) return false;
@@ -43,7 +43,7 @@ public static boolean isMajorityElement(int[] a, int n) {
4343
* @param n
4444
* @return
4545
*/
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) {
4747
if (low <= high) {
4848
int mid = (low + high) / 2;
4949
/**
@@ -53,12 +53,12 @@ public static int getIndexOf(int[] a, int low, int high, int n) {
5353
* (i) mid == 0 and a[mid] == n
5454
* (ii) n > a[mid-1] and a[mid] == n
5555
*/
56-
if ((mid == 0 || n > a[mid - 1]) && (a[mid] == n)) {
56+
if (a[mid] == n && (mid == 0 || n > a[mid - 1])) {
5757
return mid;
5858
} else if (n <= a[mid]) {
59-
return getIndexOf(a, low, mid - 1, n);
59+
return getFirstIndexOf(a, n, low, mid - 1);
6060
} else {
61-
return getIndexOf(a, mid + 1, high, n);
61+
return getFirstIndexOf(a, n, mid + 1, high);
6262
}
6363
}
6464
return -1;

0 commit comments

Comments
 (0)