Count indices where the maximum in the prefix array is less than that in the suffix array | Function to print the count of indices in which the maximum in prefix arrays is less than that in the suffix array ; If size of array is 1 ; pre [ ] : Prefix array suf [ ] : Suffix array ; Stores the required count ; Find the maximum in prefix array ; Find the maximum in suffix array ; Traverse the array ; If maximum in prefix array is less than maximum in the suffix array ; Print the answer ; Driver Code ; Function Call
def count ( a , n ) : NEW_LINE INDENT if ( n == 1 ) : NEW_LINE INDENT print ( 0 ) NEW_LINE return NEW_LINE DEDENT pre = [ 0 ] * ( n - 1 ) NEW_LINE suf = [ 0 ] * ( n - 1 ) NEW_LINE max = a [ 0 ] NEW_LINE ans = 0 NEW_LINE pre [ 0 ] = a [ 0 ] NEW_LINE for i in range ( n - 1 ) : NEW_LINE INDENT if ( a [ i ] > max ) : NEW_LINE INDENT max = a [ i ] NEW_LINE DEDENT pre [ i ] = max NEW_LINE DEDENT max = a [ n - 1 ] NEW_LINE suf [ n - 2 ] = a [ n - 1 ] NEW_LINE for i in range ( n - 2 , 0 , - 1 ) : NEW_LINE INDENT if ( a [ i ] > max ) : NEW_LINE INDENT max = a [ i ] NEW_LINE DEDENT suf [ i - 1 ] = max NEW_LINE DEDENT for i in range ( n - 1 ) : NEW_LINE INDENT if ( pre [ i ] < suf [ i ] ) : NEW_LINE INDENT ans += 1 NEW_LINE DEDENT DEDENT print ( ans ) NEW_LINE DEDENT arr = [ 2 , 3 , 4 , 8 , 1 , 4 ] NEW_LINE N = len ( arr ) NEW_LINE count ( arr , N ) NEW_LINE