java program to find Largest sum contiguous subarray

Brute Force Solution:

public class LargestSumSubarray {
    public static void main(String[] args) {
        int[] arr = { -2, -3, 4, -1, -2, 1, 5, -3 };
        int largestSum = findLargestSumSubarray(arr);
        System.out.println("Largest sum contiguous subarray: " + largestSum);
    }

    public static int findLargestSumSubarray(int[] arr) {
        int n = arr.length;
        int largestSum = Integer.MIN_VALUE;

        for (int i = 0; i < n; i++) {
            int sum = 0;
            for (int j = i; j < n; j++) {
                sum += arr[j];
                if (sum > largestSum) {
                    largestSum = sum;
                }
            }
        }

        return largestSum;
    }
}

In this code, we first declare an array arr with some sample values. We then call the findLargestSumSubarray function, which takes the array as input, and returns the largest sum contiguous subarray.

Inside the findLargestSumSubarray function, we use two nested loops to iterate through each subarray of the input array. For each subarray, we compute the sum of its elements, and update the largestSum variable if the sum is greater than the current value of largestSum.

The time complexity of this algorithm is O(n^2), since we are iterating through each subarray using two nested loops.

Now let's look at an optimal solution to find the largest sum contiguous subarray:

Optimal Solution:

public class LargestSumSubarray {
    public static void main(String[] args) {
        int[] arr = { -2, -3, 4, -1, -2, 1, 5, -3 };
        int largestSum = findLargestSumSubarray(arr);
        System.out.println("Largest sum contiguous subarray: " + largestSum);
    }

    public static int findLargestSumSubarray(int[] arr) {
        int n = arr.length;
        int largestSum = Integer.MIN_VALUE; // max_so_far
        int currentSum = arr[0]; // max_ending_here

        for (int i = 1; i < n; i++) {
            currentSum = Math.max(currentSum + arr[i], arr[i]);
            largestSum = Math.max(largestSum, currentSum);
        }

        return largestSum;
    }
}

In this optimized solution, we use the Kadane's algorithm to find the largest sum contiguous subarray. We first declare an array arr with some sample values. We then call the findLargestSumSubarray function, which takes the array as input, and returns the largest sum contiguous subarray.

Inside the findLargestSumSubarray function, we use two variables largestSum and currentSum. The largestSum variable keeps track of the largest sum contiguous subarray found so far, and the currentSum variable keeps track of the sum of the current subarray.

We start by initializing both largestSum and currentSum to the first element of the array. We then iterate through the array starting from the second element, and for each element, we update the currentSum variable by taking the maximum of the sum of the current element and the previous subarray sum, and the current element itself.We then update the largestSum variable

static void maxSubArraySum(int[] arr) {
        int max_so_far = Integer.MIN_VALUE;
        int max_ending_here = 0;
        int start = 0, end = 0, s = 0;

        for (int i = 0; i < arr.length; i++) {
            max_ending_here = max_ending_here + arr[i];
            if (max_so_far < max_ending_here) {
                max_so_far = max_ending_here;
                start = s;
                end = i;
            }
            if (max_ending_here < 0) {
                max_ending_here = 0;
                s = i + 1;
            }
        }

        System.out.println("Maximum contiguous sum is " + max_so_far);
        System.out.println("Starting index " + start);
        System.out.println("Ending index " + end);
    }