From 13fe1f06c66fd28e44a2bf75a0c533f3b558adc6 Mon Sep 17 00:00:00 2001 From: RONAK CHAKRAWATI Date: Wed, 18 Mar 2026 19:30:10 +0530 Subject: [PATCH] Handle edge case for empty or single-element vector Add a check for empty or single-element vector in binarySearch. --- search/binary_search.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/search/binary_search.cpp b/search/binary_search.cpp index ebe488e5658..9ae2634dc6d 100644 --- a/search/binary_search.cpp +++ b/search/binary_search.cpp @@ -59,6 +59,7 @@ namespace binary_search { * @returns @param int index of val in vector arr *******************************************************************************/ uint64_t binarySearch(std::vector arr, uint64_t val) { + if (arr.size() <= 1) return arr[0]; uint64_t low = 0; // set the lowest point of the vector. uint64_t high = arr.size() - 1; // set the highest point of the vector.