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
function PermuteTheArray ( A , n ) { var arr = Array ( n ) ; for ( var i = 0 ; i < n ; i ++ ) { arr [ A [ i ] - 1 ] = i ; } var mini = n , maxi = 0 ; var count = 0 ; for ( var i = 0 ; i < n ; i ++ ) { mini = Math . min ( mini , arr [ i ] ) ; maxi = Math . max ( maxi , arr [ i ] ) ; if ( maxi - mini == i ) count ++ ; } return count ; } var A = [ 4 , 5 , 1 , 3 , 2 , 6 ] ; document . write ( PermuteTheArray ( A , 6 ) ) ;