Skip to content

Commit 22a4cc4

Browse files
authored
Create Matchsticks to Square.cpp
1 parent a4553f3 commit 22a4cc4

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
typedef long long ll;
2+
class Solution {
3+
public:
4+
bool ways(vector<int> match,ll i,ll s1,ll s2,ll s3,ll s4){
5+
if(s1==0 && s2==0 && s3==0 && s4==0 && i==match.size()) return true;
6+
else if(i>=match.size()) return false;
7+
else if(s1<0 || s2<0 || s3<0 || s4<0) return false;
8+
else{
9+
return (ways(match,i+1,s1-match[i],s2,s3,s4) or ways(match,i+1,s1,s2-match[i],s3,s4) or ways(match,i+1,s1,s2,s3-match[i],s4) or ways(match,i+1,s1,s2,s3,s4-match[i]));
10+
}
11+
}
12+
bool makesquare(vector<int>& match) {
13+
ll i,j,k,n,m,ct=0,sum=0;
14+
n= match.size();
15+
for(i=0;i<n;i++) sum += match[i];
16+
if(sum%4==0){
17+
ll sidelength = sum/4;
18+
return ways(match,0,sidelength,sidelength,sidelength,sidelength);
19+
}
20+
else return false;
21+
}
22+
};

0 commit comments

Comments
 (0)