From bcb2c974ddcb45b25a2081f639319022f1a3bbd4 Mon Sep 17 00:00:00 2001 From: agrawaladitya192 Date: Tue, 6 Oct 2020 15:09:07 +0530 Subject: [PATCH 1/2] Updated bsearch.cpp to avoid integer overflow --- Search/BinarySearch/bsearch.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Search/BinarySearch/bsearch.cpp b/Search/BinarySearch/bsearch.cpp index c1ea79d..fe0d265 100644 --- a/Search/BinarySearch/bsearch.cpp +++ b/Search/BinarySearch/bsearch.cpp @@ -13,9 +13,9 @@ bool binary_search(int number) int lower = 0; int mid; - do + while(lower < higher) { - mid = (higher + lower) / 2; + mid = lower + (higher - lower) / 2; //To avoid integer overflow when adding lower and higher if (number == myarray[mid]) { std::cout << "found at pos: " << mid + 1 << std::endl; @@ -29,7 +29,7 @@ bool binary_search(int number) if (higher - lower == 1) return false; - } while (true); + } } From 2ea1e8847dfebef737f1ede80f7da6357eca326f Mon Sep 17 00:00:00 2001 From: agrawaladitya192 Date: Tue, 6 Oct 2020 15:23:00 +0530 Subject: [PATCH 2/2] Added Kadane's Algorithm Code --- Dynamic Programming/kadane_algorithm.cpp | 25 ++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 Dynamic Programming/kadane_algorithm.cpp diff --git a/Dynamic Programming/kadane_algorithm.cpp b/Dynamic Programming/kadane_algorithm.cpp new file mode 100644 index 0000000..534bd2f --- /dev/null +++ b/Dynamic Programming/kadane_algorithm.cpp @@ -0,0 +1,25 @@ +#include +#include +#include +#include +using namespace std; +int main(){ + int N; + cin>>N; + vector v(N); + for(int i=0;i>v[i]; + } + int currSum = 0,finSum = INT_MIN; + for(int i=0;i