w3resource

Java Array Exercises: Find subarray which has the largest sum in a given circular array of integers

Java Array: Exercise-67 with Solution

Write a Java program to find subarray which has the largest sum in a given circular array of integers.

Example:
Input :
nums1 = { 2, 1, -5, 4, -3, 1, -3, 4, -1 }
nums2 = { 1, -2, 3, 0, 7, 8, 1, 2, -3 }
Output:
The sum of subarray with the largest sum is 6
The sum of subarray with the largest sum is 21

Sample Solution:

Java Code:

import java.util.Arrays;
class solution {
    public static int max_Subarray_Sum_Circular(int[] nums) {
        int n = nums.length;
        int result = nums[0];
        int sum = nums[0];
        for (int i = 1; i < n; i++) {
            sum = Math.max(sum + nums[i], nums[i]);
            result = Math.max(result, sum);
        }
        
        int[] right_Sum = new int[n];
        int[] right_Max = new int[n];
        right_Sum[n - 1] = nums[n - 1];
        right_Max[n - 1] = nums[n - 1];
        for (int i = n - 2; i >= 0; i--) {
            right_Sum[i] = right_Sum[i + 1] + nums[i];
            right_Max[i] = Math.max(right_Sum[i], right_Max[i + 1]);
        }
        
        int left_Sum = 0;
        for (int i = 0; i < n - 2; i++) {
            left_Sum += nums[i];
            result = Math.max(result, left_Sum + right_Max[i + 2]);
        }
        
        return result;
    }
	
	public static void main(String[] args)
	{
		int[] nums1 = { 2, 1, -5, 4, -3, 1, -3, 4, -1 };
		System.out.println("\nOriginal circular array: "+Arrays.toString(nums1));
		System.out.println("The sum of subarray with the largest sum is " + max_Subarray_Sum_Circular(nums1));
		
		int[] nums2 = { 1, -2, 3, 0, 7, 8, 1, 2, -3 };
		System.out.println("\nOriginal circular array: "+Arrays.toString(nums2));
		System.out.println("The sum of subarray with the largest sum is " + max_Subarray_Sum_Circular(nums2));
	}
}

Sample Output:

Original circular array: [2, 1, -5, 4, -3, 1, -3, 4, -1]
The sum of subarray with the largest sum is 6

Original circular array: [1, -2, 3, 0, 7, 8, 1, 2, -3]
The sum of subarray with the largest sum is 21

Flowchart:

Flowchart: Replace each element of the array with product of every other element in a given array of integers.

Java Code Editor:

Improve this sample solution and post your code through Disqus

Previous: Write a Java program to find contiguous subarray within a given array of integers which has the largest sum.
Next: Write a Java program to create all possible permutations of a given array of distinct integers.

What is the difficulty level of this exercise?



Java: Tips of the Day

Array vs ArrayLists:

The main difference between these two is that an Array is of fixed size so once you have created an Array you cannot change it but the ArrayList is not of fixed size. You can create instances of ArrayLists without specifying its size. So if you create such instances of an ArrayList without specifying its size Java will create an instance of an ArrayList of default size.

Once an ArrayList is full it re-sizes itself. In fact, an ArrayList is internally supported by an array. So when an ArrayList is resized it will slow down its performance a bit as the contents of the old Array must be copied to a new Array.

At the same time, it's compulsory to specify the size of an Array directly or indirectly while creating it. And also Arrays can store both primitives and objects while ArrayLists only can store objects.

Ref: https://bit.ly/3o8L2KH