-
Notifications
You must be signed in to change notification settings - Fork 496
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
Modified code Largest Sum Contiguous Subarray [C, Java] #421
Closed
Closed
Changes from 7 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
1c935d7
modified code
b893e26
Merge branch 'master' of github.com:iiitv/algos into tmp
3ea7adb
remove trailing space
b9a8cf2
updated naming
71bd648
remove trailing space
0fc04f2
issue fixed
d82e13f
Update largestSumContiguousSubarray.c
a77e981
update function name
366bee2
Merge branch 'master' into tmp
aviaryan 4463282
Rename file
5d5d320
max using ternary operator
97d242f
Merge branch 'master' into tmp
singhpratyush 8c9fb73
Merge branch 'master' into tmp
singhpratyush 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 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
26 changes: 14 additions & 12 deletions
26
largest_sum_contiguous_subarray/largestSumContiguousSubarray.c
This file contains 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,25 +1,27 @@ | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
|
||
int largestSumContinousSubArray(int arr[], int size) { | ||
int largestSumContiguousSubArray(int arr[], int size) { | ||
int max_till = 0; | ||
int max_int = 0; | ||
for (int i = 0; i < size; i++) { | ||
max_int = max_int + arr[i]; | ||
if(max_till < max_int) { | ||
max_till = max_int; | ||
} | ||
if (max_int < 0) { | ||
max_int = 0; | ||
} | ||
max_int = max(max_int, arr[i]); | ||
max_till = max(max_till, max_int); | ||
} | ||
return max_till; | ||
} | ||
|
||
int main() { | ||
int array[10]; | ||
for (int k = 0; k < 10; k++) { | ||
array[k] = rand() % 10; | ||
int max(int first, int second) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Better use this. |
||
if (first < second) { | ||
return second; | ||
} else { | ||
return first; | ||
} | ||
printf("%d", largestSumContinousSubArray(array, 10)); | ||
} | ||
|
||
int main() { | ||
int array[] = {-2, 1, -3, 4, -1, 2, 1, -5, 4, 5}; | ||
int size = sizeof(array) / sizeof(array[0]); | ||
printf("%d\n", largestSumContiguousSubArray(array, size)); | ||
} |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It should be
largestSumContiguousSubarray
. i.e. same as filename.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@jsroyal We should rename the file to
...SubArray.c