w3resource

Java: Find minimum subarray sum of specified size in a given array of integers

Java Array: Exercise-69 with Solution

Write a Java program to find the minimum subarray sum of specified size in a given array of integers.

Example:
Input :
nums = { 1, 2, 3, 4, 5, 6, 7, 8, 9,10}
Output:
Sub-array size: 4
Sub-array from 0 to 3 and sum is: 10

Sample Solution:

Java Code:

// Import the necessary Java classes.
import java.util.*;

// Define the 'solution' class.
class solution {
    // Method to find the subarray with the minimum sum of size 'k'.
    public static int[] find_min_subarray_sum(int[] nums, int k) {
        // Initialize variables to track the subarray sum and its minimum.
        int sub_arr_sum = 0;
        int min_sub_arr = Integer.MAX_VALUE;
        int last = 0;
        int[] result = new int[3]; // Store the result [start, end, min sum].

        // Iterate through the 'nums' array.
        for (int i = 0; i < nums.length; i++) {
            sub_arr_sum += nums[i];

            // Check if the subarray size 'k' is reached.
            if (i + 1 >= k) {
                // Update the minimum subarray sum and its endpoint.
                if (min_sub_arr > sub_arr_sum) {
                    min_sub_arr = sub_arr_sum;
                    last = i;
                }

                // Remove the contribution of the first element in the subarray.
                sub_arr_sum -= nums[i + 1 - k];
            }
        }

        // Store the results in the 'result' array.
        result[0] = last - k + 1; // Start index of the minimum subarray.
        result[1] = last; // End index of the minimum subarray.
        result[2] = min_sub_arr; // Minimum subarray sum.
        return result;
    }

    // Main method to demonstrate finding the minimum sum subarray.
    public static void main(String[] args) {
        // Initialize the 'nums' array and subarray size 'k'.
        int[] nums = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
        int k = 4;

        // Print the original array and subarray size.
        System.out.printf("\nOriginal array: " + Arrays.toString(nums));
        System.out.printf("\nSub-array size: %d", k);

        // Find and print the minimum sum subarray.
        int[] result = find_min_subarray_sum(nums, k);
        System.out.printf("\nSub-array from %d to %d and sum is: %d", result[0], result[1], result[2]);
    }
}

Sample Output:

Original array: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
Sub-array size: 4
Sub-array from 0 to 3 and sum is: 10

Flowchart:

Flowchart: Find minimum subarray sum of specified size in a given array of integers.

Java Code Editor:

Previous: Write a Java program to create all possible permutations of a given array of distinct integers.
Next: Write a Java program to find the smallest length of a contiguous subarray of which the sum is greater than or equal to specified value. Return 0 instead.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://w3resource.com/java-exercises/array/java-array-exercise-69.php