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
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,9 @@ public static int largestSumContiguousSubarray(int[] array) { // maximum sum
prevSum = array[0]; // initialize current sum amd previous sum
currentSum = array[0];
for (i = 1; i < array.length; i++) {
Copy link
Member

@aashutoshrathi aashutoshrathi Jul 23, 2017

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 like for (int i = 1; i < array.length; i++) and remove line 13

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
}
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
21 changes: 11 additions & 10 deletions largest_sum_contiguous_subarray/largestSumContiguousSubarray.c
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
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;
}
}

int main() {
int array[] = {-2, 1, -3, 4, -1, 2, 1, -5, 4, 5};
printf("%d", largestSumContinousSubArray(array, 10));
Copy link
Member

@aashutoshrathi aashutoshrathi Jul 23, 2017

Choose a reason for hiding this comment

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

Either change Continous to Continuous (as previous one is type) or Contiguous (as filename suggests).

Copy link
Member

Choose a reason for hiding this comment

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

Instead of 10 place size there; where size = sizeof(array)/sizeof(array[0]);

Copy link
Member

Choose a reason for hiding this comment

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

Instead of %d use %d\n

}