Skip to content
This repository was archived by the owner on Nov 25, 2020. It is now read-only.
Merged
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
54 changes: 54 additions & 0 deletions Kadane/kadane.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
//MAXIMUM SUM OF SUBARRAY(CONTIGUOUS SUBSEQUENCE) KADANE'S ALGO
//Sample Input:
//Enter length of Array : 4
//Enter elements of Array
//4 5 3 5
//Sample Output:
//17

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sample input output

#include <bits/stdc++.h>

using namespace std;

int kadMax(int arr[], int size)
{
int final_max = INT_MIN, current_max = 0; //INT_MIN = MOST NEGATIVE INTEGER

for(int i = 0; i < size; i++)
{
//Iterate over Array adding each element to current max
current_max = current_max + arr[i];

//finalMax updated when less than currentMax
if(final_max < current_max)
final_max = current_max;

//If currentMax becomes negative it is reassigned value 0
if(current_max < 0)
current_max = 0;
}

return final_max;
}

int main()
{
int len, ans;

cout << "Enter length of Array : ";

cin >> len;

int ar[len];

cout<<"Enter elements of Array\n";

for(int i = 0; i < len; i++) //Array Input
cin >> ar[i];

ans = kadMax(ar, len); //Function calling

cout<<ans<<endl;

return 0;
}