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

Modified code Largest Sum Contiguous Subarray [C, Java] #421

Closed
wants to merge 13 commits into from
18 changes: 6 additions & 12 deletions largest_sum_contiguous_subarray/LargestSumContiguousSubarray.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,12 @@
public class LargestSumContiguousSubarray {

public static int largestSumContiguousSubarray(int[] array) { // maximum sum method implemention
int prevSum;
int currentSum;
int i;
prevSum = array[0]; // initialize current sum amd previous sum
currentSum = array[0];
for (i = 1; i < array.length; i++) {
currentSum += array[i]; // add values in current sum
if (currentSum < 0) { // if current sum is negative , make it zero
currentSum = 0;
} else if (currentSum > prevSum) { // if current sum is greate than previous sum
prevSum = currentSum; // update previous sum
}
int prevSum = array[0]; // initialize current sum amd previous sum
int currentSum = array[0];
for (int i = 1; i < array.length; i++) {
currentSum += array[i]; // add values in current sum
currentSum = Math.max(currentSum, array[i]); // maximum from current sum and current array value
prevSum = Math.max(currentSum, prevSum); // maximum from current sum and previous sum
}
return prevSum;
}
Expand Down
26 changes: 14 additions & 12 deletions largest_sum_contiguous_subarray/largestSumContiguousSubarray.c
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) {
Copy link
Member

@aashutoshrathi aashutoshrathi Jul 28, 2017

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.

Copy link
Member

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

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) {
Copy link
Member

Choose a reason for hiding this comment

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

#define max(a,b) (((a)>(b)) ? (a) : (b))

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));
}