From 762f0a9102191b4c5da6ed819e8e5bfacdf4381d Mon Sep 17 00:00:00 2001 From: "Paulo H. Lamounier" <53798700+Nanashii76@users.noreply.github.com> Date: Mon, 20 May 2024 22:09:26 -0300 Subject: [PATCH] some leetcode, we're back --- leetcode_problemset/problemsets/subsets.cpp | 30 +++++++++++++++++++ .../problemsets/sum_of_all_subset_xor.cpp | 30 +++++++++++++++++++ 2 files changed, 60 insertions(+) create mode 100644 leetcode_problemset/problemsets/subsets.cpp create mode 100644 leetcode_problemset/problemsets/sum_of_all_subset_xor.cpp diff --git a/leetcode_problemset/problemsets/subsets.cpp b/leetcode_problemset/problemsets/subsets.cpp new file mode 100644 index 0000000..d70da56 --- /dev/null +++ b/leetcode_problemset/problemsets/subsets.cpp @@ -0,0 +1,30 @@ +#include +using namespace std; + +vector> getAllSubsets(vector &arr, vector &out, int i, int n) { + + vector> ans; + + if (i >= n) { + ans.emplace_back(out); + return ans; + } + + // not pick + vector> 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> pick = getAllSubsets(arr, out, i + 1, n); + ans.insert(ans.end(), pick.begin(), pick.end()); + out.pop_back(); + + return ans; +} + +vector> subsets(vector& nums) { + vector out; + vector> res = getAllSubsets(nums, out, 0, nums.size()); + return res; +} diff --git a/leetcode_problemset/problemsets/sum_of_all_subset_xor.cpp b/leetcode_problemset/problemsets/sum_of_all_subset_xor.cpp new file mode 100644 index 0000000..11c4cd2 --- /dev/null +++ b/leetcode_problemset/problemsets/sum_of_all_subset_xor.cpp @@ -0,0 +1,30 @@ +#include +using namespace std; + +int getAllSubsets(vector &arr, vector &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 &nums){ + + vector ans; + return getAllSubsets(nums,ans,0,nums.size()); + +} \ No newline at end of file