Maximize subarrays count containing the maximum and minimum Array element after deleting at most one element | Returns the count of subarrays which contains both the maximum and minimum elements in the given vector ; Initialize the low and high of array ; If current element is less than least element ; If current element is more than highest element ; If current element is equal to low or high then update the pointers ; Update number of subarrays ; Return the result ; Function to find the maximum count of subarrays ; Iterate the array to find the maximum and minimum element ; Vector after removing the minimum element ; Using assignment operator to copy one vector to other ; Vector after removing the maximum element ; Given array ; Function Call
def proc ( v ) : NEW_LINE INDENT n = len ( v ) ; NEW_LINE low = v [ n - 1 ] NEW_LINE high = v [ n - 1 ] NEW_LINE p1 = n NEW_LINE p2 = n ; NEW_LINE ans = 0 ; NEW_LINE for i in range ( n - 1 , - 1 , - 1 ) : NEW_LINE INDENT x = v [ i ] ; NEW_LINE if ( x < low ) : NEW_LINE low = x ; NEW_LINE ans = 0 ; NEW_LINE elif ( x > high ) : NEW_LINE high = x ; NEW_LINE ans = 0 ; NEW_LINE if ( x == low ) : p1 = i ; NEW_LINE if ( x == high ) : p2 = i ; NEW_LINE ans += n - max ( p1 , p2 ) ; NEW_LINE DEDENT return ans ; NEW_LINE DEDENT def subarray ( v ) : NEW_LINE INDENT n = len ( v ) ; NEW_LINE if ( n <= 1 ) : NEW_LINE INDENT return n ; NEW_LINE DEDENT ans = proc ( v ) ; NEW_LINE low = v [ 0 ] NEW_LINE pos_low = 0 NEW_LINE high = v [ 0 ] NEW_LINE pos_high = 0 NEW_LINE for i in range ( 1 , n ) : NEW_LINE INDENT x = v [ i ] ; NEW_LINE if ( x < low ) : NEW_LINE low = x ; NEW_LINE pos_low = i ; NEW_LINE elif ( x > high ) : NEW_LINE high = x ; NEW_LINE pos_high = i ; NEW_LINE DEDENT u = v [ : ] ; NEW_LINE del u [ pos_low ] ; NEW_LINE ans = max ( ans , proc ( u ) ) ; NEW_LINE w = v [ : ] ; NEW_LINE del w [ pos_high ] ; NEW_LINE return max ( ans , proc ( w ) ) ; NEW_LINE DEDENT v = [ ] ; NEW_LINE v . append ( 7 ) ; NEW_LINE v . append ( 2 ) ; NEW_LINE v . append ( 5 ) ; NEW_LINE v . append ( 4 ) ; NEW_LINE v . append ( 3 ) ; NEW_LINE v . append ( 1 ) ; NEW_LINE print ( subarray ( v ) ) ; NEW_LINE