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

Conversation

sundaram123krishnan
Copy link

@sundaram123krishnan sundaram123krishnan commented Apr 16, 2023

This change attempts to resolve issue #1030.
Only included the implementation of the algorithm, let me know whether i should further continue to contribute or not.
Signed-off-by : Sundaram krishnan krishnsundaram@gmail.com

@sundaram123krishnan sundaram123krishnan changed the title Added implementatin of kadane's algorithm Added implementatin for kadane's algorithm Apr 16, 2023
*/

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.

* Simplest Kadane's algorithm implementation.
*
* @param {*[]} 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}

/**
* 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


// This algorithm works for both positive and negative values of the array

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

// This algorithm works for both positive and negative values of the array

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants