|
1 | 1 | package backtracking;
|
| 2 | + |
2 | 3 | import java.util.*;
|
| 4 | + |
3 | 5 | /**
|
4 | 6 | * Created by gouthamvidyapradhan on 25/05/2019 Remember the story of Little Match Girl? By now, you
|
5 | 7 | * know exactly what matchsticks the little match girl has, please find out a way you can make one
|
|
19 | 21 | * sum of the given matchsticks is in the range of 0 to 10^9. The length of the given matchstick
|
20 | 22 | * array will not exceed 15.
|
21 | 23 | *
|
22 |
| -* Solution: O(2 ^ N): Generate a power set of all combination of numbers for the given array which sum up to the |
23 |
| -* length of a side of square. |
24 |
| -* Now, to check if a square can be made using all the sides sticks of different length, generate a hash for for each of |
25 |
| -* the combination which was generated in the previous step. The hash function should be such that it uses unique |
26 |
| -* indexes of each match stick. If 4 different hash values are formed using unique and all indices then a square is |
27 |
| -* possible. |
| 24 | +* <p>Solution: O(2 ^ N): Generate a power set of all combination of numbers for the given array |
| 25 | +* which sum up to the length of a side of square. Now, to check if a square can be made using all |
| 26 | +* the sides sticks of different length, generate a hash for for each of the combination which was |
| 27 | +* generated in the previous step. The hash function should be such that it uses unique indexes of |
| 28 | +* each match stick. If 4 different hash values are formed using unique and all indices then a |
| 29 | +* square is possible. |
28 | 30 | */
|
29 | 31 | public class MatchsticksToSquare {
|
30 |
| -/** |
31 |
| -* Main method |
32 |
| -* @param args |
33 |
| -*/ |
| 32 | +/** |
| 33 | +* Main method |
| 34 | +* |
| 35 | +* @param args |
| 36 | +*/ |
34 | 37 | public static void main(String[] args) {
|
35 |
| -int[] A = {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 6, 10, 10}; |
| 38 | +int[] A = {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 6, 10, 10}; |
36 | 39 | System.out.println(new MatchsticksToSquare().makesquare(A));
|
37 | 40 | }
|
38 | 41 |
|
39 |
| -class Pair { |
40 |
| -int value, i; |
41 |
| -Pair(int value, int i){ |
42 |
| -this.value = value; |
43 |
| -this.i = i; |
44 |
| -} |
| 42 | +class Pair { |
| 43 | +int value, i; |
| 44 | + |
| 45 | +Pair(int value, int i) { |
| 46 | +this.value = value; |
| 47 | +this.i = i; |
45 | 48 | }
|
| 49 | +} |
46 | 50 |
|
47 |
| -public boolean makesquare(int[] nums) { |
48 |
| -if(nums.length == 0) return false; |
49 |
| -int sum = 0; |
50 |
| -for(int n : nums){ |
51 |
| -sum += n; |
52 |
| -} |
53 |
| -int side = sum / 4; |
54 |
| -if((sum % 4) != 0) return false; |
55 |
| -List<List<Pair>> list = powerSet(nums, side); |
56 |
| -Set<Integer> hashIndex = new HashSet<>(); |
57 |
| -int cons = 0; |
58 |
| -for(int i = 0; i < nums.length; i ++){ |
59 |
| -cons |= (1 << i); |
| 51 | +public boolean makesquare(int[] nums) { |
| 52 | +if (nums.length == 0) return false; |
| 53 | +int sum = 0; |
| 54 | +for (int n : nums) { |
| 55 | +sum += n; |
| 56 | +} |
| 57 | +int side = sum / 4; |
| 58 | +if ((sum % 4) != 0) return false; |
| 59 | +List<List<Pair>> list = powerSet(nums, side); |
| 60 | +Set<Integer> hashIndex = new HashSet<>(); |
| 61 | +int cons = 0; |
| 62 | +for (int i = 0; i < nums.length; i++) { |
| 63 | +cons |= (1 << i); |
| 64 | +} |
| 65 | +for (int i = 0; i < list.size(); i++) { |
| 66 | +for (int j = i + 1; j < list.size(); j++) { |
| 67 | +Set<Integer> indexList = new HashSet<>(); |
| 68 | +List<Pair> list1 = list.get(i); |
| 69 | +List<Pair> list2 = list.get(j); |
| 70 | +int hash = 0; |
| 71 | +for (Pair l1 : list1) { |
| 72 | +indexList.add(l1.i); |
| 73 | +hash |= (1 << l1.i); |
60 | 74 | }
|
61 |
| -for(int i = 0; i < list.size(); i ++){ |
62 |
| -for(int j = i + 1; j < list.size(); j ++){ |
63 |
| -Set<Integer> indexList = new HashSet<>(); |
64 |
| -List<Pair> list1 = list.get(i); |
65 |
| -List<Pair> list2 = list.get(j); |
66 |
| -int hash = 0; |
67 |
| -for(Pair l1 : list1){ |
68 |
| -indexList.add(l1.i); |
69 |
| -hash |= (1 << l1.i); |
70 |
| -} |
71 |
| -boolean allUnique = true; |
72 |
| -for(Pair l2 : list2){ |
73 |
| -if(indexList.contains(l2.i)) { |
74 |
| -allUnique = false; |
75 |
| -break; |
76 |
| -} |
77 |
| -indexList.add(l2.i); |
78 |
| -hash |= (1 << l2.i); |
79 |
| -} |
80 |
| -if(allUnique){ |
81 |
| -hashIndex.add(hash); |
82 |
| -int complement = ((~ hash) & cons); |
83 |
| -if(hashIndex.contains(complement)) return true; |
84 |
| -} |
| 75 | +boolean allUnique = true; |
| 76 | +for (Pair l2 : list2) { |
| 77 | +if (indexList.contains(l2.i)) { |
| 78 | +allUnique = false; |
| 79 | +break; |
85 | 80 | }
|
| 81 | +indexList.add(l2.i); |
| 82 | +hash |= (1 << l2.i); |
| 83 | +} |
| 84 | +if (allUnique) { |
| 85 | +hashIndex.add(hash); |
| 86 | +int complement = ((~hash) & cons); |
| 87 | +if (hashIndex.contains(complement)) return true; |
| 88 | +} |
86 | 89 | }
|
87 |
| -return false; |
88 | 90 | }
|
| 91 | +return false; |
| 92 | +} |
89 | 93 |
|
90 |
| -private List<List<Pair>> powerSet(int[] nums, int expectedSum){ |
91 |
| -List<List<Pair>> result = new ArrayList<>(); |
92 |
| -generate(0, nums, new ArrayList<>(), result, 0, expectedSum); |
93 |
| -return result; |
94 |
| -} |
| 94 | +private List<List<Pair>> powerSet(int[] nums, int expectedSum) { |
| 95 | +List<List<Pair>> result = new ArrayList<>(); |
| 96 | +generate(0, nums, new ArrayList<>(), result, 0, expectedSum); |
| 97 | +return result; |
| 98 | +} |
95 | 99 |
|
96 |
| -private void generate(int i, int[] nums, List<Pair> subList, List<List<Pair>> result, int sum, |
97 |
| -int expected){ |
98 |
| -if(i >= nums.length){ |
99 |
| -if(sum == expected){ |
100 |
| -List<Pair> pairs = new ArrayList<>(subList); |
101 |
| -result.add(pairs); |
102 |
| -} |
103 |
| -} else{ |
104 |
| -if(sum + nums[i] <= expected){ |
105 |
| -subList.add(new Pair(nums[i], i)); |
106 |
| -generate(i + 1, nums, subList, result, sum + nums[i], expected); |
107 |
| -subList.remove(subList.size() - 1); |
108 |
| -} |
109 |
| -generate(i + 1, nums, subList, result, sum, expected); |
| 100 | +private void generate( |
| 101 | +int i, int[] nums, List<Pair> subList, List<List<Pair>> result, int sum, int expected) { |
| 102 | +if (i >= nums.length) { |
| 103 | +if (sum == expected) { |
| 104 | +List<Pair> pairs = new ArrayList<>(subList); |
| 105 | +result.add(pairs); |
110 | 106 | }
|
| 107 | +} else { |
| 108 | +if (sum + nums[i] <= expected) { |
| 109 | +subList.add(new Pair(nums[i], i)); |
| 110 | +generate(i + 1, nums, subList, result, sum + nums[i], expected); |
| 111 | +subList.remove(subList.size() - 1); |
| 112 | +} |
| 113 | +generate(i + 1, nums, subList, result, sum, expected); |
111 | 114 | }
|
| 115 | +} |
112 | 116 | }
|
0 commit comments