Skip to content
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

Added implementatin for kadane's algorithm #1031

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
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
Prev Previous commit
Next Next commit
fixed an error in max function
sundaram123krishnan committed Apr 16, 2023
commit 5bd5f80c7812c8527163b2c6283c72b54822600a
Original file line number Diff line number Diff line change
@@ -10,8 +10,8 @@ export default function kadane() {
let maximum_subarray_sum = array[0];
Copy link

Choose a reason for hiding this comment

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

Suggested change
let maximum_subarray_sum = array[0];
let maximum_subarray_sum = array[0] ?? -Infinity;

or

Suggested change
let maximum_subarray_sum = array[0];
let maximum_subarray_sum = array[0] ?? - Math.pow(2, 53) - 1;


for (let i = 0; i < array.length; i++) {
current_sum = max(current_sum + array[i], array[i]);
maximum_subarray_sum = max(maximum_subarray_sum, current_sum);
current_sum = Math.max(current_sum + array[i], array[i]);
maximum_subarray_sum = Math.max(maximum_subarray_sum, current_sum);
}

return maximum_subarray_sum;