text: Count of subarrays of size K which is a permutation of numbers from 1 to K | Javascript implementation ; Save index of numbers of the array ; Update min and max index with the current index and check if it 's a valid permutation ; Driver Code
code: function find_permutations ( arr ) { var cnt = 0 ; var max_ind = - 1 , min_ind = 10000000 ; var n = arr . length ; var index_of = new Map ( ) ; for ( var i = 0 ; i < n ; i ++ ) { index_of . set ( arr [ i ] , i + 1 ) ; } for ( var i = 1 ; i <= n ; i ++ ) { max_ind = Math . max ( max_ind , index_of . get ( i ) ) ; min_ind = Math . min ( min_ind , index_of . get ( i ) ) ; if ( max_ind - min_ind + 1 == i ) cnt ++ ; } return cnt ; } var nums = [ ] ; nums . push ( 2 ) ; nums . push ( 3 ) ; nums . push ( 1 ) ; nums . push ( 5 ) ; nums . push ( 4 ) ; document . write ( find_permutations ( nums ) ) ;