Skip to content

Commit 9100078

Browse files
AdityaAditya
Aditya
authored and
Aditya
committed
Fixed the underFlow bug in src/binarySearch.sol
1 parent 1f48039 commit 9100078

File tree

1 file changed

+9
-5
lines changed

1 file changed

+9
-5
lines changed

src/Searches/BinarySearch.sol

+9-5
Original file line numberDiff line numberDiff line change
@@ -36,15 +36,19 @@ contract BinarySearch {
3636
uint256 currentElement = _nums[middle];
3737
if (currentElement < value) {
3838
minimum = middle + 1;
39-
} else if (currentElement > value) {
40-
maximum = middle - 1;
39+
}
40+
else if (currentElement > value) {
41+
if (middle == 0) {
42+
maximum = 0;
43+
} else {
44+
maximum = middle - 1;
45+
}
4146
} else {
42-
result = middle;
47+
result = int256(middle);
4348
// return the index position of the value in the array
4449
return result;
4550
}
4651
}
47-
// return 0 if value is not in the array
48-
return 0;
52+
return -1;
4953
}
5054
}

0 commit comments

Comments
 (0)