diff --git a/largest_sum_contiguous_subarray/LargestSumContiguousSubArray.java b/largest_sum_contiguous_subarray/LargestSumContiguousSubArray.java new file mode 100644 index 00000000..ecfbf32b --- /dev/null +++ b/largest_sum_contiguous_subarray/LargestSumContiguousSubArray.java @@ -0,0 +1,26 @@ +/* + * Implementation of famous dynamic programming problem + * Largest Sum Contiguous Subarray + * Kadane Algorithm + * Time complexity O(n) + */ + +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]; + 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; + } + + 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)); + } +} diff --git a/largest_sum_contiguous_subarray/LargestSumContiguousSubarray.java b/largest_sum_contiguous_subarray/LargestSumContiguousSubarray.java deleted file mode 100644 index 74b08eed..00000000 --- a/largest_sum_contiguous_subarray/LargestSumContiguousSubarray.java +++ /dev/null @@ -1,32 +0,0 @@ -/* - * Implementation of famous dynamic programming problem - * Largest Sum Contiguous Subarray - * Kadane Algorithm - * Time complexity O(n) - */ - -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 - } - } - return prevSum; - } - - 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)); - } -} diff --git a/largest_sum_contiguous_subarray/largestSumContiguousSubarray.c b/largest_sum_contiguous_subarray/largestSumContiguousSubarray.c index 2b2594b6..1694f884 100644 --- a/largest_sum_contiguous_subarray/largestSumContiguousSubarray.c +++ b/largest_sum_contiguous_subarray/largestSumContiguousSubarray.c @@ -1,25 +1,20 @@ #include #include +#define max(a,b) (((a)>(b)) ? (a) : (b)) -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; - } - printf("%d", largestSumContinousSubArray(array, 10)); + 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)); }