Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 6a481cb

Browse files
Merge pull request #116 from thisabhijeet/modify_kadanes
kadens.cpp has been modified to add comments, example test cases and proper variable names
2 parents cdbf6b0 + e45ef69 commit 6a481cb

File tree

1 file changed

+62
-15
lines changed

1 file changed

+62
-15
lines changed

‎03-Arrays/kadens.cpp‎

Lines changed: 62 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,71 @@
1-
#include<iostream>
1+
/**
2+
* Our task is to take length of array and then the whole array as input from
3+
* the user and then calculate the maximum contiguous subarray sum for the
4+
* input array, using the kadane's algorithm.
5+
*
6+
* There can be a case that all the elements in the input array are negative.
7+
* In that case, the least value among all elements is the maximum sum with
8+
* subarray length = 1.
9+
*/
10+
11+
#include <climits> // for INT_MIN value
12+
#include <iostream> // for IO operations
213
using namespace std;
3-
int main()
14+
15+
/**
16+
* max_suarray_sum function calculates the maximum contiguous sum for the given array.
17+
*/
18+
int max_subarray_sum(int arr[], int length)
419
{
5-
int cs=0;
6-
int ms=0;
7-
int n;cin>>n;
8-
int a[100];
9-
for(int i=0;i<n;i++)
20+
int current_max = INT_MIN, current_sum = 0;
21+
for (int i = 0; i < length; i++)
1022
{
11-
cin>>a[i];
12-
23+
current_sum = current_sum + arr[i];
24+
if (current_max < current_sum)
25+
{
26+
current_max = current_sum;
27+
}
28+
29+
if (current_sum < 0)
30+
{
31+
current_sum = 0;
32+
}
1333
}
14-
for(int i=0;i<n;i++)
34+
return current_max;
35+
}
36+
37+
/**
38+
* example test cases
39+
* {
40+
* arr = {1, 2, 3, 4}
41+
* maximum contiguos subarray sum = 1 +たす 2 +たす 3 +たす 4 = 10
42+
* arr1 = {-1, -2, -4, -6, 7}
43+
* maximum contiguos subarray sum = 7
44+
* arr1 = {-1, -2, -4, -6, -7}
45+
* maximum contiguos subarray sum = -1
46+
* }
47+
*/
48+
49+
// main function
50+
int main()
51+
{
52+
// code for accepting array from user starts
53+
54+
int n; // variable for length of input array
55+
cout << "Enter length of the array: ";
56+
cin >> n;
57+
int arr[n]; // array to store the input array
58+
59+
for (int i = 0; i < n; i++) //taking elements of the array
1560
{
16-
cs= cs+a[i];
17-
if(cs<0)
61+
1862
{
19-
cs=0;
63+
cin >> arr[i];
2064
}
21-
ms= max(ms,cs);
2265
}
23-
cout<<"MAXIMUM SUM IS:"<<ms<<endl;
66+
// code for accepting array from user ends
67+
int max_sum = max_subarray_sum(arr, n); // max_sum stores the maximum contiguous subarray sum
68+
cout << "Maximum contiguous sum for this array is : " << max_sum << endl;
69+
70+
return 0;
2471
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /