From 91000784b912e6c57621f78ee61fc2c4a165b62e Mon Sep 17 00:00:00 2001 From: Aditya Date: Mon, 2 Oct 2023 23:28:06 +0530 Subject: [PATCH] Fixed the underFlow bug in src/binarySearch.sol --- src/Searches/BinarySearch.sol | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/src/Searches/BinarySearch.sol b/src/Searches/BinarySearch.sol index ead3f44..b5cb184 100644 --- a/src/Searches/BinarySearch.sol +++ b/src/Searches/BinarySearch.sol @@ -36,15 +36,19 @@ contract BinarySearch { uint256 currentElement = _nums[middle]; if (currentElement < value) { minimum = middle + 1; - } else if (currentElement > value) { - maximum = middle - 1; + } + else if (currentElement > value) { + if (middle == 0) { + maximum = 0; + } else { + maximum = middle - 1; + } } else { - result = middle; + result = int256(middle); // return the index position of the value in the array return result; } } - // return 0 if value is not in the array - return 0; + return -1; } }