Skip to content

Commit e7fbe44

Browse files
committed
fix(searches): find first occurrence in LowerBound with duplicate values
When multiple elements equal to key exist, LowerBound should return the index of the first such value per its specification. Continue searching the left partition when comp <= 0 instead of prematurely returning the median element.
1 parent d9b86d1 commit e7fbe44

1 file changed

Lines changed: 2 additions & 4 deletions

File tree

‎src/main/java/com/thealgorithms/searches/LowerBound.java‎

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,8 @@ private <T extends Comparable<T>> int search(T[] array, T key, int left, int rig
5151
int median = (left + right) >>> 1;
5252
int comp = key.compareTo(array[median]);
5353

54-
if (comp == 0) {
55-
return median;
56-
} else if (comp < 0) {
57-
// median position can be a possible solution
54+
if (comp <= 0) {
55+
// key is less than or equal to array[median], median position can be a possible solution
5856
return search(array, key, left, median);
5957
} else {
6058
// key we are looking is greater, so we must look on the right of median position

0 commit comments

Comments
 (0)