-
-
Notifications
You must be signed in to change notification settings - Fork 5.7k
added documentation, js standard style and jest testing to kadane algorithm file. #750
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
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
3a8e336
added documentation, js standard style and jest testing to kadane alg...
thisabhijeet c0fa140
Merge branch 'kadanes' of https://github.com/thisabhijeet/Javascript ...
thisabhijeet 5a59d1e
changes made
thisabhijeet ed49e12
tried to fix tests
thisabhijeet File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,25 @@ | ||
function KadaneAlgo (array) { | ||
/* Kadane's algorithm is one of the most efficient ways to | ||
* calculate the maximum contiguos subarray sum for a given array. | ||
* Below is the implementation of kadanes's algorithm along with | ||
* some sample test cases. | ||
* There might be a special case in this problem if al the elements | ||
* of the given array are negative. In such a case, the maximum negative | ||
* value present in the array is the answer. | ||
* | ||
* Reference article :- https://www.geeksforgeeks.org/largest-sum-contiguous-subarray/ | ||
*/ | ||
|
||
export function kadaneAlgo (array) { | ||
let cummulativeSum = 0 | ||
let maxSum = 0 | ||
let maxSum = Number.NEGATIVE_INFINITY // maxSum has the least posible value | ||
for (let i = 0; i < array.length; i++) { | ||
cummulativeSum = cummulativeSum + array[i] | ||
if (cummulativeSum < 0) { | ||
cummulativeSum = 0 | ||
} else if (maxSum < cummulativeSum) { | ||
if (maxSum < cummulativeSum) { | ||
maxSum = cummulativeSum | ||
} else if (cummulativeSum < 0) { | ||
cummulativeSum = 0 | ||
} | ||
} | ||
return maxSum | ||
// This function returns largest sum contiguous sum in a array | ||
} | ||
function main () { | ||
const myArray = [1, 2, 3, 4, -6] | ||
const result = KadaneAlgo(myArray) | ||
console.log(result) | ||
} | ||
main() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import { kadaneAlgo } from '../KadaneAlgo' | ||
test('it is being checked that 15 is the answer to the corresponding array input', () => { | ||
expect(kadaneAlgo([1, 2, 3, 4, 5])).toBe(15) | ||
}) | ||
|
||
test('it is being checked that 5 is the answer to the corresponding array input', () => { | ||
expect(kadaneAlgo([-1, -2, -3, -4, 5])).toBe(5) | ||
}) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.