text: Count of permutations of an Array having each element as a multiple or a factor of its index | Java 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 final count ; Driver Code
code: import java . util . * ; class GFG { static int findPermutation ( Set < Integer > arr , int N ) { int pos = arr . size ( ) + 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 ; Set < Integer > arr = new HashSet < Integer > ( ) ; System . out . print ( findPermutation ( arr , N ) ) ; } }