-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3d04c35
commit 762f0a9
Showing
2 changed files
with
60 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
#include <bits/stdc++.h> | ||
using namespace std; | ||
|
||
vector<vector<int>> getAllSubsets(vector<int> &arr, vector<int> &out, int i, int n) { | ||
|
||
vector<vector<int>> ans; | ||
|
||
if (i >= n) { | ||
ans.emplace_back(out); | ||
return ans; | ||
} | ||
|
||
// not pick | ||
vector<vector<int>> not_pick = getAllSubsets(arr, out, i + 1, n); | ||
ans.insert(ans.end(), not_pick.begin(), not_pick.end()); | ||
|
||
// pick | ||
out.emplace_back(arr[i]); | ||
vector<vector<int>> pick = getAllSubsets(arr, out, i + 1, n); | ||
ans.insert(ans.end(), pick.begin(), pick.end()); | ||
out.pop_back(); | ||
|
||
return ans; | ||
} | ||
|
||
vector<vector<int>> subsets(vector<int>& nums) { | ||
vector<int> out; | ||
vector<vector<int>> res = getAllSubsets(nums, out, 0, nums.size()); | ||
return res; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
#include <bits/stdc++.h> | ||
using namespace std; | ||
|
||
int getAllSubsets(vector<int> &arr, vector<int> &ans, int i, int n) { | ||
|
||
if(i >= n) { | ||
int xor_cal = 0; | ||
for(int k = 0; k < (int)ans.size(); ++k) { | ||
xor_cal ^= ans[k]; | ||
} | ||
return xor_cal; | ||
} | ||
|
||
// not pick | ||
int not_pick = getAllSubsets(arr,ans,i+1,n); | ||
|
||
// pick | ||
ans.emplace_back(arr[i]); | ||
int pick = getAllSubsets(arr,ans,i+1,n); | ||
ans.pop_back(); | ||
|
||
return not_pick + pick; | ||
} | ||
|
||
int subsetXORSum(vector<int> &nums){ | ||
|
||
vector<int> ans; | ||
return getAllSubsets(nums,ans,0,nums.size()); | ||
|
||
} |