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

Create Maximum-Sum-Subarray-[Kadanes-Algorithm] #825

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
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
23 changes: 23 additions & 0 deletions Data-Structures/Array/Maximum-Sum-Subarray-[Kadanes-Algorithm]
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
Given an array of integers containing positive as well as negative integers. Find the sum of subarray which has maximum sum, in the given array.

Input : 2,-3,4,5,-1
Output : 9 (Subarray - [2,3])

This is O(N) in time complexity.
*/

let findMaxSumSubarray = (arr) => {
let final_max = arr[0];
let curr_max = arr[0];

for (let i = 1; i < arr.length; i++) {
let current = arr[i];
final_max = Math.max(current, current + final_max);
curr_max = Math.max(curr_max, final_max);
}
return curr_max;
};

let res = findMaxSumSubarray([2,-3,4,5,-1]);
console.log(res);

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