Largest sum in contiguous subarray
Given an array find the contiguous subarray with maximum sum: I) If the array consists of both positive and negative elements: We will have the sum initialize the sum to zero. If we find the sum greater then zero then we will update the sum. In successive steps we will update the maximum sum only when it is greater than previous sum. Code: import java.util.*; public class Solution { public static void main(String[] args) { int[] a = {-2, -3, 4, -1, -2, 1, 5, -3}; int n = a.length; int sum = 0; int newsum = 0; for(int i=0; i<n; i++){ newsum += a[i]; if(newsum<0){ newsum = 0; ...