-
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
Changes from 3 commits
1c935d7
b893e26
3ea7adb
b9a8cf2
71bd648
0fc04f2
d82e13f
a77e981
366bee2
4463282
5d5d320
97d242f
8c9fb73
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,20 +6,21 @@ int largestSumContinousSubArray(int arr[], int size) { | |
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; | ||
} | ||
} | ||
|
||
int main() { | ||
int array[] = {-2, 1, -3, 4, -1, 2, 1, -5, 4, 5}; | ||
printf("%d", largestSumContinousSubArray(array, 10)); | ||
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. Either change 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. Instead of 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. Instead of |
||
} |
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.
i
can be declared here only likefor (int i = 1; i < array.length; i++)
and remove line 13