<p>You are given an integer array <code>matchsticks</code> where <code>matchsticks[i]</code> is the length of the <code>i<sup>th</sup></code> matchstick. You want to use <strong>all the matchsticks</strong> to make one square. You <strong>should not break</strong> any stick, but you can link them up, and each matchstick must be used <strong>exactly one time</strong>.</p>

<p>Return <code>true</code> if you can make this square and <code>false</code> otherwise.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/04/09/matchsticks1-grid.jpg" style="width: 253px; height: 253px;" />
<pre>
<strong>Input:</strong> matchsticks = [1,1,2,2,2]
<strong>Output:</strong> true
<strong>Explanation:</strong> You can form a square with length 2, one side of the square came two sticks with length 1.
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> matchsticks = [3,3,3,3,4]
<strong>Output:</strong> false
<strong>Explanation:</strong> You cannot find a way to form a square with all the matchsticks.
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>1 &lt;= matchsticks.length &lt;= 15</code></li>
	<li><code>1 &lt;= matchsticks[i] &lt;= 10<sup>8</sup></code></li>
</ul>

// class Solution {
// public:
//     bool helper(vector<int>& matchsticks , vector<int> &sides , int index , int val){
//         if(index==matchsticks.size()){
//             return true;
//         }
//         for(int i=0 ; i<4 ; i++){
//             if(sides[i] + matchsticks[index] <= val){
//                 sides[i]+=matchsticks[index];
//                 if(helper(matchsticks , sides , index+1 , val)){
//                     return true;
//                 }
//                 sides[i]-=matchsticks[index];
//             }
//         }
//         return false;
//     }
//     bool makesquare(vector<int>& matchsticks) {
//         int sum=0;
//         for(auto el : matchsticks){
//             sum+=el;
//         }
//         int val=0;
//         if(sum%4!=0){
//             return false;
//         }
//         val=sum/4;
//         vector<int> sides(4 , 0);
//         return helper(matchsticks , sides , 0 , val);
//     }
// };
// class Solution {
// public:
//     bool dp(vector<int>& matchsticks, vector<int>& sides, int index, int target) {
//         if (index == matchsticks.size()) {
//             // Check if all sides are equal to the target length
//             return sides[0] == target && sides[1] == target && sides[2] == target && sides[3] == target;
//         }

//         // Try to place the current matchstick in any of the 4 sides
//         for (int i = 0; i < 4; ++i) {
//             if (sides[i] + matchsticks[index] <= target) {
//                 sides[i] += matchsticks[index];

//                 if (dp(matchsticks, sides, index + 1, target)) {
//                     return true;
//                 }

//                 sides[i] -= matchsticks[index];  // backtrack
//             }

//             // If one side is 0 and we can't place the matchstick, there's no point in trying other sides
//             if (sides[i] == 0) break;
//         }

//         return false;
//     }

//     bool makesquare(vector<int>& matchsticks) {
//         int sum = 0;
//         for (int el : matchsticks) {
//             sum += el;
//         }

//         // If the total sum is not divisible by 4, it's impossible to form a square
//         if (sum % 4 != 0) {
//             return false;
//         }

//         int target = sum / 4;
//         vector<int> sides(4, 0);
        
//         // Sort the matchsticks in descending order to optimize backtracking
//         sort(matchsticks.rbegin(), matchsticks.rend());

//         // Start the backtracking process from the first matchstick
//         return dp(matchsticks, sides, 0, target);
//     }
// };
class Solution {
public:
    unordered_map<int, bool> memo;
    
    bool dp(vector<int>& matchsticks, int usedMask, int currentSum, int target, int sidesFormed) {
        // If 3 sides are formed, the last side must also be the same, so return true
        if (sidesFormed == 3) return true;
        
        // Check if the current state has been computed before
        if (memo.find(usedMask) != memo.end()) {
            return memo[usedMask];
        }
        
        // Try placing each unused matchstick
        for (int i = 0; i < matchsticks.size(); ++i) {
            if (usedMask & (1 << i)) continue; // If matchstick i is already used, skip it

            // If placing this matchstick doesn't exceed the side length
            if (currentSum + matchsticks[i] <= target) {
                // Try placing matchstick[i] into the current side
                if (dp(matchsticks, usedMask | (1 << i), (currentSum + matchsticks[i]) % target, target, sidesFormed + ((currentSum + matchsticks[i]) == target))) {
                    return memo[usedMask] = true;
                }
            }
        }

        return memo[usedMask] = false;
    }
    
    bool makesquare(vector<int>& matchsticks) {
        int sum = accumulate(matchsticks.begin(), matchsticks.end(), 0);
        
        // If the total length is not divisible by 4, we can't form a square
        if (sum % 4 != 0) {
            return false;
        }
        
        int target = sum / 4;
        memo.clear();
        
        // Sort matchsticks in descending order to attempt to place larger sticks first
        sort(matchsticks.rbegin(), matchsticks.rend());
        
        // Start recursive backtracking with memoization
        return dp(matchsticks, 0, 0, target, 0);
    }
};

24946