Length of the smallest subarray with maximum possible sum
Given an array arr[] consisting of N non-negative integers, the task is to find the minimum length of the subarray whose sum is maximum.
Example:
Input: arr[] = {0, 2, 0, 0, 12, 0, 0, 0}
Output: 4
Explanation: The sum of the subarray {2, 0, 0, 12} = 2 + 0 + 0 + 12 = 14, which is maximum sum possible and the length of the subarray is 4, which is minimum.Input: arr[] = {2, 0, 0}
Output: 1
Naive Approach: The simplest approach to solve the given problem is to generate all possible subarray of the given array and print the length of that subarray whose sum is the sum of the array having minimum length among all possible subarray of the same sum.
Time Complexity: O(N2)
Auxiliary Space: O(1)
Efficient Approach: The above approach can be optimized by using the fact that all elements are non-negative so the maximum sum of the subarray is the sum of the array itself. Therefore, the idea is to exclude the trailing 0s from either end of the array to minimize the resultant subarray length with maximum sum. Follow the steps below to solve the problem:
- Initialize two variables, say i as 0 and j as (N – 1) that store the starting and the ending index of the resultant subarray.
- Traverse the given array from the front until a positive element is encountered and simultaneously increment the value of i.
- If the value of i is N, then print the maximum length of the resultant subarray as 1 because it contains all elements as 0. Otherwise, traverse the given array from the end until a positive element is encountered and simultaneously decrement the value of j.
- After completing the above steps, print the value of (j – i + 1) as the resultant subarray.
Below is the implementation of the above approach:
C++
// C++ program for the above approach #include <bits/stdc++.h> using namespace std; // Function to find the minimum length // of the subarray whose sum is maximum int minimumSizeSubarray( int arr[], int N) { // Stores the starting and the // ending index of the resultant // subarray int i = 0, j = N - 1; // Traverse the array until a // non-zero element is encountered while (i < N and arr[i] == 0) { i++; } // If the array contains only of 0s if (i == N) return 1; // Traverse the array in reverse until // a non-zero element is encountered while (j >= 0 and arr[j] == 0) { j--; } // Return the resultant // size of the subarray return (j - i + 1); } // Driver Code int main() { int arr[] = { 0, 2, 0, 0, 12, 0, 0, 0 }; int N = sizeof (arr) / sizeof (arr[0]); cout << minimumSizeSubarray(arr, N); return 0; } |
Java
// Java program for the above approach import java.util.*; class GFG { // Function to find the minimum length // of the subarray whose sum is maximum static int minimumSizeSubarray( int arr[], int N) { // Stores the starting and the // ending index of the resultant // subarray int i = 0 , j = N - 1 ; // Traverse the array until a // non-zero element is encountered while (i < N && arr[i] == 0 ) { i++; } // If the array contains only of 0s if (i == N) return 1 ; // Traverse the array in reverse until // a non-zero element is encountered while (j >= 0 && arr[j] == 0 ) { j--; } // Return the resultant // size of the subarray return (j - i + 1 ); } // Driver Code public static void main(String[] args) { int arr[] = { 0 , 2 , 0 , 0 , 12 , 0 , 0 , 0 }; int N = arr.length; System.out.print( minimumSizeSubarray(arr, N)); } } // This code is contributed by code_hunt. |
Python3
# Python3 program for the above approach # Function to find the minimum length # of the subarray whose sum is maximum def minimumSizeSubarray(arr, N): # Stores the starting and the # ending index of the resultant # subarray i, j = 0 , N - 1 # Traverse the array until a # non-zero element is encountered while (i < N and arr[i] = = 0 ): i + = 1 # If the array contains only of 0s if (i = = N): return 1 # Traverse the array in reverse until # a non-zero element is encountered while (j > = 0 and arr[j] = = 0 ): j - = 1 # Return the resultant # size of the subarray return (j - i + 1 ) # Driver Code if __name__ = = '__main__' : arr = [ 0 , 2 , 0 , 0 , 12 , 0 , 0 , 0 ] N = len (arr) print (minimumSizeSubarray(arr, N)) # This code is contributed by mohit kumar 29 |
C#
// C# program for the above approach using System; class GFG{ // Function to find the minimum length // of the subarray whose sum is maximum static int minimumSizeSubarray( int [] arr, int N) { // Stores the starting and the // ending index of the resultant // subarray int i = 0, j = N - 1; // Traverse the array until a // non-zero element is encountered while (i < N && arr[i] == 0) { i++; } // If the array contains only of 0s if (i == N) return 1; // Traverse the array in reverse until // a non-zero element is encountered while (j >= 0 && arr[j] == 0) { j--; } // Return the resultant // size of the subarray return (j - i + 1); } // Driver code public static void Main() { int [] arr = { 0, 2, 0, 0, 12, 0, 0, 0 }; int N = arr.Length; Console.Write( minimumSizeSubarray(arr, N)); } } // This code is contributed by code_hunt |
Javascript
<script> // JavaScript program for the above approach // Function to find the minimum length // of the subarray whose sum is maximum function minimumSizeSubarray(arr, N) { // Stores the starting and the // ending index of the resultant // subarray let i = 0, j = N - 1; // Traverse the array until a // non-zero element is encountered while (i < N && arr[i] == 0) { i++; } // If the array contains only of 0s if (i == N) return 1; // Traverse the array in reverse until // a non-zero element is encountered while (j >= 0 && arr[j] == 0) { j--; } // Return the resultant // size of the subarray return (j - i + 1); } // Driver Code let arr = [ 0, 2, 0, 0, 12, 0, 0, 0 ]; let N = arr.length; document.write(minimumSizeSubarray(arr, N)); </script> |
4
Time Complexity: O(N)
Auxiliary Space: O(1)
Please Login to comment...