Count of subarrays which forms a permutation from given Array elements | Function returns the required count ; Store the indices of the elements present in A [ ] . ; Store the maximum and minimum index of the elements from 1 to i . ; Update maxi and mini , to store minimum and maximum index for permutation of elements from 1 to i + 1 ; If difference between maxi and mini is equal to i ; Increase count ; Return final count ; Driver Code
def PermuteTheArray ( A , n ) : NEW_LINE INDENT arr = [ 0 ] * n NEW_LINE for i in range ( n ) : NEW_LINE INDENT arr [ A [ i ] - 1 ] = i NEW_LINE DEDENT mini = n NEW_LINE maxi = 0 NEW_LINE count = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT mini = min ( mini , arr [ i ] ) NEW_LINE maxi = max ( maxi , arr [ i ] ) NEW_LINE if ( maxi - mini == i ) : NEW_LINE INDENT count += 1 NEW_LINE DEDENT DEDENT return count NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT A = [ 4 , 5 , 1 , 3 , 2 , 6 ] NEW_LINE print ( PermuteTheArray ( A , 6 ) ) NEW_LINE DEDENT