forked from rohan472000/Java-DSA-InterviewPrep
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathisSquareFul.java
45 lines (43 loc) · 1.47 KB
/
isSquareFul.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class isSquareFul {
public static void main(String[] args) {
int[] arr = {1,17,8};
System.out.println(permut(arr));
}
private static int count = 0;
public static int permut(int[] nums){
//List<List<Integer>> res = new ArrayList<>();
if(nums.length == 0 || nums == null) return count;
Arrays.sort(nums);
boolean[] used = new boolean[nums.length];
List<Integer> permutations = new ArrayList<>();
helper(permutations,nums,used,-1);
return count;
}
private static boolean isSquare(int a, int b){
double res = Math.sqrt(a+b);
boolean finalres = (res - Math.floor(res)) == 0;
return finalres;
}
private static void helper(List<Integer> temp, int[] nums, boolean[] used, int lastNumb) {
if (temp.size() == nums.length) count++;
else {
for (int i = 0; i < nums.length; i++) {
if(used[i]) continue;
if(lastNumb!=-1){
if (isSquare(nums[i],lastNumb)==false)
continue;
}
used[i] = true;
temp.add(nums[i]);
helper(temp,nums,used,lastNumb);
temp.remove(temp.size()-1);
used[i] = false;
while(i+1<nums.length && nums[i] == nums[i+1])
++i;
}
}
}
}