Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions Dynamic Programming/kadane_algorithm.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#include <iostream>
#include <vector>
#include <algorithm>
#include <climits>
using namespace std;
int main(){
int N;
cin>>N;
vector<int> v(N);
for(int i=0;i<N;i++){
cin>>v[i];
}
int currSum = 0,finSum = INT_MIN;
for(int i=0;i<N;i++){
currSum += v[i];
currSum = max(currSum,0);
finSum = max(finSum,currSum);
}
//If the input contains all negative numbers, the sum of subarray will be zero, so return the max element among the negative numbers.
if(finSum==0){
cout<<*max_element(v.begin(),v.end())<<endl;
}else{
cout<<finSum<<endl;
}
}
6 changes: 3 additions & 3 deletions Search/BinarySearch/bsearch.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -29,7 +29,7 @@ bool binary_search(int number)

if (higher - lower == 1)
return false;
} while (true);
}
}


Expand Down