package com.fishercoder.solutions;

/**
 * 526. Beautiful Arrangement
 *
 * Suppose you have N integers from 1 to N.
 * We define a beautiful arrangement as an array that is constructed by these N numbers successfully
 * if one of the following is true for the ith position (1 ≤ i ≤ N) in this array:
 * The number at the ith position is divisible by i.
 * i is divisible by the number at the ith position.
 * Now given N, how many beautiful arrangements can you construct?

 Example 1:

 Input: 2
 Output: 2

 Explanation:

 The first beautiful arrangement is [1, 2]:
 Number at the 1st position (i=1) is 1, and 1 is divisible by i (i=1).
 Number at the 2nd position (i=2) is 2, and 2 is divisible by i (i=2).

 The second beautiful arrangement is [2, 1]:
 Number at the 1st position (i=1) is 2, and 2 is divisible by i (i=1).
 Number at the 2nd position (i=2) is 1, and i (i=2) is divisible by 1.

 Note:
 N is a positive integer and will not exceed 15.
 */
public class _526 {
	public static class Solution1 {
		/**
		 * A good post to look at: https://discuss.leetcode.com/topic/79916/java-solution-backtracking
		 * and there's a generic template afterwards for backtracking problems
		 */

		int count = 0;

		public int countArrangement(int N) {
			backtracking(N, new int[N + 1], 1);
			return count;
		}

		private void backtracking(int N, int[] used, int pos) {
			if (pos > N) {
				count++;
				return;
			}

			for (int i = 1; i <= N; i++) {
				if (used[i] == 0 && (i % pos == 0 || pos % i == 0)) {
					used[i] = 1;
					backtracking(N, used, pos + 1);
					used[i] = 0;
				}
			}
		}
	}

}

@@ -33,993 +33,84 @@
 s;%0A%0A
-/**%0A * 526. Beautiful Arrangement%0A *%0A * Suppose you have N integers from 1 to N.%0A * We define a beautiful arrangement as an array that is constructed by these N numbers successfully%0A * if one of the following is true for the ith position (1 %E2%89%A4 i %E2%89%A4 N) in this array:%0A * The number at the ith position is divisible by i.%0A * i is divisible by the number at the ith position.%0A * Now given N, how many beautiful arrangements can you construct?%0A%0A Example 1:%0A%0A Input: 2%0A Output: 2%0A%0A Explanation:%0A%0A The first beautiful arrangement is %5B1, 2%5D:%0A Number at the 1st position (i=1) is 1, and 1 is divisible by i (i=1).%0A Number at the 2nd position (i=2) is 2, and 2 is divisible by i (i=2).%0A%0A The second beautiful arrangement is %5B2, 1%5D:%0A Number at the 1st position (i=1) is 2, and 2 is divisible by i (i=1).%0A Number at the 2nd position (i=2) is 1, and i (i=2) is divisible by 1.%0A%0A Note:%0A N is a positive integer and will not exceed 15.%0A */%0Apublic class _526 %7B%0A%09public static class Solution1 %7B%0A%09%09/**%0A%09%09
+public class _526 %7B%0A    public static class Solution1 %7B%0A        /**%0A        
  * A
@@ -200,18 +200,24 @@
 racking%0A
-%09%09
+        
  * and t
@@ -283,17 +283,29 @@
 ems%0A
-%09%09 */%0A%0A%09%09
+         */%0A%0A        
 int 
@@ -316,18 +316,24 @@
 t = 0;%0A%0A
-%09%09
+        
 public i
@@ -361,19 +361,28 @@
 nt N) %7B%0A
-%09%09%09
+            
 backtrac
@@ -409,19 +409,28 @@
 1%5D, 1);%0A
-%09%09%09
+            
 return c
@@ -435,23 +435,35 @@
  count;%0A
-%09%09%7D%0A%0A%09%09
+        %7D%0A%0A        
 private 
@@ -510,19 +510,28 @@
  pos) %7B%0A
-%09%09%09
+            
 if (pos 
@@ -541,42 +541,84 @@
 ) %7B%0A
-%09%09%09%09count++;%0A%09%09%09%09return;%0A%09%09%09%7D%0A%0A%09%09%09
+                count++;%0A                return;%0A            %7D%0A%0A            
 for 
@@ -644,20 +644,32 @@
  i++) %7B%0A
-%09%09%09%09
+                
 if (used
@@ -718,97 +718,172 @@
 ) %7B%0A
-%09%09%09%09%09used%5Bi%5D = 1;%0A%09%09%09%09%09backtracking(N, used, pos + 1);%0A%09%09%09%09%09used%5Bi%5D = 0;%0A%09%09%09%09%7D%0A%09%09%09%7D%0A%09%09%7D%0A%09
+                    used%5Bi%5D = 1;%0A                    backtracking(N, used, pos + 1);%0A                    used%5Bi%5D = 0;%0A                %7D%0A            %7D%0A        %7D%0A    
 %7D%0A%0A%7D

refactor 526