Skip to content

Commit

Permalink
added Kadane's Algorithm in C++
Browse files Browse the repository at this point in the history
  • Loading branch information
amitShindeGit committed Oct 1, 2022
1 parent 63324d7 commit f88fcff
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 0 deletions.
58 changes: 58 additions & 0 deletions C++/Kadanes_Algorithm.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
// Github Username: amitShindeGit
#include <iostream>
using namespace std;

class Solution{
public:
// arr: input array
// n: size of array
//Function to find the sum of contiguous subarray with maximum sum.
long long max(long long x, long long y){
if(x > y){
return x;
}else{
return y;
}
}

long long maxSubarraySum(int arr[], int n){

// Your code here
long long max_current = arr[0];
long long max_global = arr[0];

for(int i=1; i<=n-1; i++){
max_current = max(arr[i], max_current + arr[i]);
// cout << max_current << " " << max_global << endl;

if(max_current > max_global){
max_global = max_current;
}
}

return max_global;

}
};


int main()
{
int t,n;

cin>>t; //input testcases
while(t--) //while testcases exist
{

cin>>n; //input size of array

int a[n];

for(int i=0;i<n;i++)
cin>>a[i]; //inputting elements of array

Solution ob;

cout << ob.maxSubarraySum(a, n) << endl;
}
}
1 change: 1 addition & 0 deletions CONTRIBUTORS.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
| Sarthak Roy | <a href="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/sarthakroy2002">Sarthak Roy</a> | <a href="mailto:[email protected]">E-Mail</a> |
| Hardik Pratap Singh | <a href="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/hardik-pratap-singh">Hardik Pratap Singh</a> | <a href="mailto:[email protected]">E-Mail</a> |
| Suraj Bhandarkar S | <a href="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/Suraj-Bhandarkar-S">Adarsh Addee</a> | <a href="mailto:[email protected]">E-Mail</a> |
| Amit Shinde | <a href="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/amitShindeGit">Amit Shinde</a> | <a href="mailto:[email protected]">E-Mail</a> |

<br>
<h1>
Expand Down

0 comments on commit f88fcff

Please sign in to comment.