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

Different between parseInt() and valueOf() in java?

Well, the API for Integer.valueOf(String) does indeed say that the String is interpreted exactly as if it were given to Integer.parseInt(String). However, valueOf(String) returns a new Integer() object whereas parseInt(String) returns a primitive int.

If you want to enjoy the potential caching benefits of Integer.valueOf(int), you could also use this eyesore:

Integer k = Integer.valueOf(Integer.parseInt("123"))

Now, if what you want is the object and not the primitive, then using valueOf(String) may be more attractive than making a new object out of parseInt(String) because the former is consistently present across Integer, Long, Double, etc.

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