text: Count of subarrays of size K which is a permutation of numbers from 1 to K | C # program to implement the above approach ; 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: using System ; using System . Collections ; using System . Collections . Generic ; class GFG { static int find_permutations ( ArrayList arr ) { int cnt = 0 ; int max_ind = - 1 , min_ind = 10000000 ; int n = arr . Count ; Dictionary < int , int > index_of = new Dictionary < int , int > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { index_of [ ( int ) arr [ i ] ] = i + 1 ; } for ( int i = 1 ; i <= n ; i ++ ) { max_ind = Math . Max ( max_ind , index_of [ i ] ) ; min_ind = Math . Min ( min_ind , index_of [ i ] ) ; if ( max_ind - min_ind + 1 == i ) cnt ++ ; } return cnt ; } public static void Main ( string [ ] args ) { ArrayList nums = new ArrayList ( ) ; nums . Add ( 2 ) ; nums . Add ( 3 ) ; nums . Add ( 1 ) ; nums . Add ( 5 ) ; nums . Add ( 4 ) ; Console . Write ( find_permutations ( nums ) ) ; } }