Length of the smallest subarray with maximum possible sum | Function to find the minimum length of the subarray whose sum is maximum ; Stores the starting and the ending index of the resultant subarray ; Traverse the array until a non - zero element is encountered ; If the array contains only of 0 s ; Traverse the array in reverse until a non - zero element is encountered ; Return the resultant size of the subarray ; Driver Code
def minimumSizeSubarray ( arr , N ) : NEW_LINE INDENT i , j = 0 , N - 1 NEW_LINE while ( i < N and arr [ i ] == 0 ) : NEW_LINE INDENT i += 1 NEW_LINE DEDENT if ( i == N ) : NEW_LINE INDENT return 1 NEW_LINE DEDENT while ( j >= 0 and arr [ j ] == 0 ) : NEW_LINE INDENT j -= 1 NEW_LINE DEDENT return ( j - i + 1 ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 0 , 2 , 0 , 0 , 12 , 0 , 0 , 0 ] NEW_LINE N = len ( arr ) NEW_LINE print ( minimumSizeSubarray ( arr , N ) ) NEW_LINE DEDENT