package Backtracking;

import java.util.LinkedList;

public class BeautifulArrangement {
    int arrCnt = 0;
    public int countArrangement(int N) {
        LinkedList<Integer> nums = new LinkedList<>();
        int i = 1;
        while(i <= N)
            nums.add(i++);
        countArrangement(1, nums);
        return arrCnt;
    }

    private void countArrangement(int idx, LinkedList<Integer> nums){
        
        if(nums.isEmpty()){
            arrCnt++;
            return;
        }

        int nextIdx = idx+1;
        for(int n : nums)
            if(n%idx == 0 || idx%n == 0){
                LinkedList<Integer> remainNums = (LinkedList<Integer>)(nums.clone());
                remainNums.remove((Object)n);
                countArrangement(nextIdx, remainNums);
            }

        return;
    }
}
@@ -155,121 +155,41 @@
     
-LinkedList%3CInteger%3E nums = new LinkedList%3C%3E();%0A        int i = 1;%0A        while(i %3C= N)%0A            nums.add(i++)
+boolean%5B%5D visted = new boolean%5BN%5D
 ;%0A  
@@ -214,20 +214,22 @@
 ment(1, 
-nums
+visted
 );%0A     
@@ -300,32 +300,24 @@
 dx, 
-LinkedList%3CInteger%3E nums
+boolean%5B%5D visted
 )%7B%0A 
@@ -339,22 +339,27 @@
  if(
-nums.isEmpty()
+idx %3E visted.length
 )%7B%0A 
@@ -422,37 +422,58 @@
     
-int nextIdx = idx+1;%0A
+for(int i = 0; i %3C visted.length; i++) %7B%0A 
         
 for(
@@ -472,25 +472,25 @@
     
-for(
+   
 int n 
-: nums)
+= i + 1;
 %0A   
@@ -500,16 +500,32 @@
       if
+ (!visted%5Bi%5D && 
 (n%25idx =
@@ -542,16 +542,18 @@
 %25n == 0)
+) 
 %7B%0A      
@@ -566,177 +566,108 @@
     
-LinkedList%3CInteger%3E remainNums = (LinkedList%3CInteger%3E)(nums.clone());%0A                remainNums.remove((Object)n);%0A                countArrangement(nextIdx, remainNums)
+visted%5Bi%5D = true;%0A                countArrangement(idx+1, visted);%0A                visted%5Bi%5D = false
 ;%0A  
@@ -670,32 +670,41 @@
 ;%0A            %7D%0A
+        %7D
 %0A        return;
@@ -711,8 +711,9 @@
 %0A    %7D%0A%7D
+%0A

Update BeautifulArrangement.java