Java: Find the smallest length of a contiguous subarray of which the sum is greater than or equal to specified value
Java Array: Exercise-70 with Solution
Write a Java program to find the smallest length of a contiguous subarray of which the sum is greater than or equal to a specified value. Return 0 instead.
Example:
Input :
nums = {1, 2, 3, 4, 6}
Output:
Minimum length of a contiguous subarray of which the sum is 8, 2
Sample Solution:
Java Code:
// Import the necessary Java classes.
import java.util.Arrays;
// Define the 'solution' class.
public class solution {
public static void main(String[] args)
{
// Initialize the 'nums' array and the target sum 'm_len'.
int[] nums = {1, 2, 3, 4, 6};
int m_len = 8;
// Find and store the minimum subarray length with a sum of 'm_len'.
int result = min_SubArray_length(m_len, nums);
// Print the original array and the minimum subarray length.
System.out.printf("\nOriginal array: " + Arrays.toString(nums));
System.out.printf("\nMinimum length of a contiguous subarray of which the sum is %d: %d", m_len, result);
}
public static int min_SubArray_length(int s, int[] nums) {
// Initialize variables for sum, counter, and minimum length.
int sum = 0, ctr = 0, min_len = Integer.MAX_VALUE;
// Iterate through the 'nums' array.
for (int i = 0, j = 0; j < nums.length; ) {
// Check if the current element is greater than or equal to the target sum 's'.
if (nums[j] >= s) {
return 1; // A single element is sufficient.
} else {
// Update the sum and counter with the current element.
sum += nums[j];
ctr++;
// Check if the current subarray sum is greater than or equal to 's'.
if (sum >= s) {
// Update the minimum length, then shrink the subarray from the front.
min_len = Math.min(min_len, ctr);
while (j > i) {
sum -= nums[i];
ctr--;
i++;
if (sum < s) break;
min_len = Math.min(min_len, ctr);
}
}
}
j++;
}
// Check if the minimum length was updated, and return it.
if (min_len == Integer.MAX_VALUE) {
return 0; // No subarray found.
}
return min_len;
}
}
Sample Output:
Original array: [1, 2, 3, 4, 6] Minimum length of a contiguous subarray of which the sum is 8, 2
Flowchart:
Java Code Editor:
Previous: Write a Java program to find minimum subarray sum of specified size in a given array of integers.
Next: Write a Java program to form the largest number from a given list of non negative integers.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
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-70.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics