text: Count of permutations of an Array having each element as a multiple or a factor of its index | C # program to implement the above approach ; Function to find the count of desired permutations ; Base case ; If i has not been inserted ; Backtrack ; Insert i ; Recur to find valid permutations ; Remove i ; Return the readonly count ; Driver Code
code: using System ; using System . Collections . Generic ; class GFG { static int findPermutation ( HashSet < int > arr , int N ) { int pos = arr . Count + 1 ; if ( pos > N ) return 1 ; int res = 0 ; for ( int i = 1 ; i <= N ; i ++ ) { if ( ! arr . Contains ( i ) ) { if ( i % pos == 0 pos % i == 0 ) { arr . Add ( i ) ; res += findPermutation ( arr , N ) ; arr . Remove ( i ) ; } } } return res ; } public static void Main ( String [ ] args ) { int N = 5 ; HashSet < int > arr = new HashSet < int > ( ) ; Console . Write ( findPermutation ( arr , N ) ) ; } }