From 071d4090f2a694a1be37ba3f05e2740843560647 Mon Sep 17 00:00:00 2001 From: PayalSah Date: Fri, 4 Feb 2022 14:37:32 +0530 Subject: [PATCH] I change on first coccurence cpp file one condition where you use for mid=high+low/2 , instead of these we can do mid=low + (high-low)/2 for better funtioning of code . --- sort_search_problems/first_occurrence_binary_search.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sort_search_problems/first_occurrence_binary_search.cpp b/sort_search_problems/first_occurrence_binary_search.cpp index 9778279..21360ad 100644 --- a/sort_search_problems/first_occurrence_binary_search.cpp +++ b/sort_search_problems/first_occurrence_binary_search.cpp @@ -13,7 +13,7 @@ int low = 0; int firstOccurrance = -1; while ( low <= high ) { - int mid = (high + low) / 2; + int mid = low + (high - low)/2; //avoiding overflow if ( key < vec[mid] ) { high = mid - 1; } else if ( key > vec[mid]) {