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 3 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
18 changes: 18 additions & 0 deletions src/algorithms/uncategorized/kadane's algorithm/kadane_algo.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/**
* Simplest Kadane's algorithm implementation.
*
* @param {*[]} array
Copy link

Choose a reason for hiding this comment

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

Suggested change
* @param {*[]} array
* @param {number[]} array

* @return {number[]}
Copy link

Choose a reason for hiding this comment

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

Suggested change
* @return {number[]}
* @return {number}

*/

export default function kadane() {
let current_sum = array[0];

Choose a reason for hiding this comment

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

what is array?

Choose a reason for hiding this comment

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

Thanks for pointing out , i forgot to add parameter to function.

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 current_sum = array[0];
let current_sum = array[0] ?? 0;

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 = 1; i < array.length; i++) {
current_sum = Math.max(current_sum + array[i], array[i]);
maximum_subarray_sum = Math.max(maximum_subarray_sum, current_sum);
}

return maximum_subarray_sum;
}