From 1c935d79b7f5b2acea9a6dcc85f867e830c25fc4 Mon Sep 17 00:00:00 2001 From: jsroyal Date: Thu, 20 Jul 2017 09:29:56 +0530 Subject: [PATCH 1/9] modified code --- .../LargestSumContiguousSubarray.java | 9 +++----- .../largestSumContiguousSubarray.c | 21 ++++++++++--------- 2 files changed, 14 insertions(+), 16 deletions(-) diff --git a/largest_sum_contiguous_subarray/LargestSumContiguousSubarray.java b/largest_sum_contiguous_subarray/LargestSumContiguousSubarray.java index 74b08eed..d570f99a 100644 --- a/largest_sum_contiguous_subarray/LargestSumContiguousSubarray.java +++ b/largest_sum_contiguous_subarray/LargestSumContiguousSubarray.java @@ -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++) { - 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; } diff --git a/largest_sum_contiguous_subarray/largestSumContiguousSubarray.c b/largest_sum_contiguous_subarray/largestSumContiguousSubarray.c index 2b2594b6..b86cf9e1 100644 --- a/largest_sum_contiguous_subarray/largestSumContiguousSubarray.c +++ b/largest_sum_contiguous_subarray/largestSumContiguousSubarray.c @@ -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) { + 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)); } From 3ea7adb547228b8a9737d726ca9f9e99af067040 Mon Sep 17 00:00:00 2001 From: jsroyal Date: Thu, 20 Jul 2017 09:51:15 +0530 Subject: [PATCH 2/9] remove trailing space --- .../LargestSumContiguousSubarray.java | 2 +- .../largestSumContiguousSubarray.c | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/largest_sum_contiguous_subarray/LargestSumContiguousSubarray.java b/largest_sum_contiguous_subarray/LargestSumContiguousSubarray.java index d570f99a..e9ac93aa 100644 --- a/largest_sum_contiguous_subarray/LargestSumContiguousSubarray.java +++ b/largest_sum_contiguous_subarray/LargestSumContiguousSubarray.java @@ -16,7 +16,7 @@ public static int largestSumContiguousSubarray(int[] array) { // maximum sum for (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 + prevSum = Math.max(currentSum, prevSum); // maximum from current sum and previous sum } return prevSum; } diff --git a/largest_sum_contiguous_subarray/largestSumContiguousSubarray.c b/largest_sum_contiguous_subarray/largestSumContiguousSubarray.c index b86cf9e1..64513011 100644 --- a/largest_sum_contiguous_subarray/largestSumContiguousSubarray.c +++ b/largest_sum_contiguous_subarray/largestSumContiguousSubarray.c @@ -7,12 +7,12 @@ int largestSumContinousSubArray(int arr[], int size) { for (int i = 0; i < size; i++) { max_int = max_int + arr[i]; max_int = max(max_int, arr[i]); - max_till = max(max_till, max_int); + max_till = max(max_till, max_int); } return max_till; } -int max(int first, int second) { +int max(int first, int second) { if (first < second) { return second; } else { From b9a8cf2989ca2fc2a29fe56daf1615cf32ee3666 Mon Sep 17 00:00:00 2001 From: jsroyal Date: Tue, 25 Jul 2017 14:01:49 +0530 Subject: [PATCH 3/9] updated naming --- .../LargestSumContiguousSubarray.java | 9 +++------ .../largestSumContiguousSubarray.c | 5 +++-- 2 files changed, 6 insertions(+), 8 deletions(-) diff --git a/largest_sum_contiguous_subarray/LargestSumContiguousSubarray.java b/largest_sum_contiguous_subarray/LargestSumContiguousSubarray.java index e9ac93aa..aa72de76 100644 --- a/largest_sum_contiguous_subarray/LargestSumContiguousSubarray.java +++ b/largest_sum_contiguous_subarray/LargestSumContiguousSubarray.java @@ -8,12 +8,9 @@ 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++) { + 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 diff --git a/largest_sum_contiguous_subarray/largestSumContiguousSubarray.c b/largest_sum_contiguous_subarray/largestSumContiguousSubarray.c index 64513011..9fc0b323 100644 --- a/largest_sum_contiguous_subarray/largestSumContiguousSubarray.c +++ b/largest_sum_contiguous_subarray/largestSumContiguousSubarray.c @@ -1,7 +1,7 @@ #include #include -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++) { @@ -22,5 +22,6 @@ int max(int first, int second) { int main() { int array[] = {-2, 1, -3, 4, -1, 2, 1, -5, 4, 5}; - printf("%d", largestSumContinousSubArray(array, 10)); + size = sizeof(array)/sizeof(array[0]); + printf("%d\n", largestSumContiguousSubArray(array, size)); } From 71bd648cee8599f260beaf6b9a545efa90bc0928 Mon Sep 17 00:00:00 2001 From: jsroyal Date: Thu, 27 Jul 2017 10:11:53 +0530 Subject: [PATCH 4/9] remove trailing space --- .../LargestSumContiguousSubarray.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/largest_sum_contiguous_subarray/LargestSumContiguousSubarray.java b/largest_sum_contiguous_subarray/LargestSumContiguousSubarray.java index aa72de76..1dd79f68 100644 --- a/largest_sum_contiguous_subarray/LargestSumContiguousSubarray.java +++ b/largest_sum_contiguous_subarray/LargestSumContiguousSubarray.java @@ -9,7 +9,7 @@ public class LargestSumContiguousSubarray { public static int largestSumContiguousSubarray(int[] array) { // maximum sum method implemention int prevSum = array[0]; // initialize current sum amd previous sum - int currentSum = array[0]; + 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 From 0fc04f204c185c39d8135af7c1477a8a28db22bc Mon Sep 17 00:00:00 2001 From: jsroyal Date: Thu, 27 Jul 2017 12:33:31 +0530 Subject: [PATCH 5/9] issue fixed --- largest_sum_contiguous_subarray/largestSumContiguousSubarray.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/largest_sum_contiguous_subarray/largestSumContiguousSubarray.c b/largest_sum_contiguous_subarray/largestSumContiguousSubarray.c index 9fc0b323..f2af5f9f 100644 --- a/largest_sum_contiguous_subarray/largestSumContiguousSubarray.c +++ b/largest_sum_contiguous_subarray/largestSumContiguousSubarray.c @@ -22,6 +22,6 @@ int max(int first, int second) { int main() { int array[] = {-2, 1, -3, 4, -1, 2, 1, -5, 4, 5}; - size = sizeof(array)/sizeof(array[0]); + int size = sizeof(array)/sizeof(array[0]); printf("%d\n", largestSumContiguousSubArray(array, size)); } From d82e13fed0d31be327e0f7c25788fc89ea75f2b7 Mon Sep 17 00:00:00 2001 From: Jitendra Singh Date: Fri, 28 Jul 2017 23:15:26 +0530 Subject: [PATCH 6/9] Update largestSumContiguousSubarray.c --- largest_sum_contiguous_subarray/largestSumContiguousSubarray.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/largest_sum_contiguous_subarray/largestSumContiguousSubarray.c b/largest_sum_contiguous_subarray/largestSumContiguousSubarray.c index f2af5f9f..21a2bff0 100644 --- a/largest_sum_contiguous_subarray/largestSumContiguousSubarray.c +++ b/largest_sum_contiguous_subarray/largestSumContiguousSubarray.c @@ -22,6 +22,6 @@ int max(int first, int second) { int main() { int array[] = {-2, 1, -3, 4, -1, 2, 1, -5, 4, 5}; - int size = sizeof(array)/sizeof(array[0]); + int size = sizeof(array) / sizeof(array[0]); printf("%d\n", largestSumContiguousSubArray(array, size)); } From a77e981891633561bb439efe89e40cae71cb5ff1 Mon Sep 17 00:00:00 2001 From: Jitendra Singh Date: Thu, 17 Aug 2017 07:11:20 +0530 Subject: [PATCH 7/9] update function name --- .../largestSumContiguousSubarray.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/largest_sum_contiguous_subarray/largestSumContiguousSubarray.c b/largest_sum_contiguous_subarray/largestSumContiguousSubarray.c index 21a2bff0..79cc2722 100644 --- a/largest_sum_contiguous_subarray/largestSumContiguousSubarray.c +++ b/largest_sum_contiguous_subarray/largestSumContiguousSubarray.c @@ -1,7 +1,7 @@ #include #include -int largestSumContiguousSubArray(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++) { @@ -23,5 +23,5 @@ int max(int first, int second) { 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)); + printf("%d\n", largestSumContiguousSubarray(array, size)); } From 44632825ff2a144de3e4a9a314fc6ea1a17e6464 Mon Sep 17 00:00:00 2001 From: Jitendra Singh Date: Mon, 21 Aug 2017 08:26:54 +0530 Subject: [PATCH 8/9] Rename file --- ...guousSubarray.java => LargestSumContiguousSubArray.java} | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) rename largest_sum_contiguous_subarray/{LargestSumContiguousSubarray.java => LargestSumContiguousSubArray.java} (78%) diff --git a/largest_sum_contiguous_subarray/LargestSumContiguousSubarray.java b/largest_sum_contiguous_subarray/LargestSumContiguousSubArray.java similarity index 78% rename from largest_sum_contiguous_subarray/LargestSumContiguousSubarray.java rename to largest_sum_contiguous_subarray/LargestSumContiguousSubArray.java index 1dd79f68..ecfbf32b 100644 --- a/largest_sum_contiguous_subarray/LargestSumContiguousSubarray.java +++ b/largest_sum_contiguous_subarray/LargestSumContiguousSubArray.java @@ -5,9 +5,9 @@ * Time complexity O(n) */ -public class LargestSumContiguousSubarray { +public class LargestSumContiguousSubArray { - public static int largestSumContiguousSubarray(int[] array) { // maximum sum method implemention + public static int largestSumContiguousSubArray(int[] array) { // maximum sum method implemention int prevSum = array[0]; // initialize current sum amd previous sum int currentSum = array[0]; for (int i = 1; i < array.length; i++) { @@ -21,6 +21,6 @@ public static int largestSumContiguousSubarray(int[] array) { // maximum sum public static void main(String[] args) { int[] array = new int[] {-2, 1, -3, 4, -1, 2, 1, -5, 4}; - System.out.println("Largest Sum of Contiguous Subarray:" + "\t" + largestSumContiguousSubarray(array)); + System.out.println("Largest Sum of Contiguous SubArray:" + "\t" + largestSumContiguousSubArray(array)); } } From 5d5d320cb7640c5740a7049b80c73473be4b1e4e Mon Sep 17 00:00:00 2001 From: Jitendra Singh Date: Mon, 21 Aug 2017 08:31:57 +0530 Subject: [PATCH 9/9] max using ternary operator --- .../largestSumContiguousSubarray.c | 9 +-------- 1 file changed, 1 insertion(+), 8 deletions(-) diff --git a/largest_sum_contiguous_subarray/largestSumContiguousSubarray.c b/largest_sum_contiguous_subarray/largestSumContiguousSubarray.c index 79cc2722..1694f884 100644 --- a/largest_sum_contiguous_subarray/largestSumContiguousSubarray.c +++ b/largest_sum_contiguous_subarray/largestSumContiguousSubarray.c @@ -1,5 +1,6 @@ #include #include +#define max(a,b) (((a)>(b)) ? (a) : (b)) int largestSumContiguousSubarray(int arr[], int size) { int max_till = 0; @@ -12,14 +13,6 @@ int largestSumContiguousSubarray(int arr[], int size) { return max_till; } -int max(int first, int second) { - if (first < second) { - return second; - } else { - return first; - } -} - int main() { int array[] = {-2, 1, -3, 4, -1, 2, 1, -5, 4, 5}; int size = sizeof(array) / sizeof(array[0]);