Concise solution by using dfs and bit manipulation


  • 0
    X
    void dfs(int curr,int N,int used, int& res)
        {
            if(curr==N+1)
                res++;
            for(int i=1;i<=N;i++)
                if(!(used&(1<<i))&&(!(i%curr)||!(curr%i)))
                    dfs(curr+1,N,used|(1<<i),res);
        }
        
        int countArrangement(int N) 
        {
            int res=0;
            dfs(1,N,0,res);
            return res;
        }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.