aboutsummaryrefslogtreecommitdiff
path: root/sum_subset_xor.c
diff options
context:
space:
mode:
authorJack Sangdahl <[email protected]>2025-04-04 23:34:14 -0600
committerJack Sangdahl <[email protected]>2025-04-04 23:34:14 -0600
commitf8d9a0c6ef10d434749cbc0d9ae51b1482aad1b6 (patch)
treea8549ed421b21c5bafa829e5cacda3ae53d9ef01 /sum_subset_xor.c
parent6e2b4dac24efabc44781551afdb9d1723849ae62 (diff)
downloadleetcode-f8d9a0c6ef10d434749cbc0d9ae51b1482aad1b6.tar.gz
leetcode-f8d9a0c6ef10d434749cbc0d9ae51b1482aad1b6.zip
1863. sum of all subset xor totals
Diffstat (limited to 'sum_subset_xor.c')
-rw-r--r--sum_subset_xor.c23
1 files changed, 4 insertions, 19 deletions
diff --git a/sum_subset_xor.c b/sum_subset_xor.c
index cb51647..7c32618 100644
--- a/sum_subset_xor.c
+++ b/sum_subset_xor.c
@@ -8,26 +8,11 @@
* if 'a' can be obtained from 'b' by deleting some elements of 'b'.
*/
-void backtrack(int *nums, int numsSize, int tar, int idx, int *cnt) {
- if (idx == numsSize) {
- return;
- } else {
- for (int i = idx; i < numsSize; i++) {
- tar ^= nums[i];
- *cnt += tar;
- backtrack(nums, numsSize, tar, i + 1, cnt);
- tar ^= nums[i];
- }
- }
-}
-
int subsetXORSum(int *nums, int numsSize) {
- int ans = 0;
- for (int i = 0; i < numsSize; i++) {
- ans += nums[i];
- backtrack(nums, numsSize, nums[i], i + 1, &ans);
- }
- return ans;
+ int all = 0, mult = 1 << (numsSize - 1);
+ for (int i = 0; i < numsSize; i++)
+ all |= nums[i];
+ return all * mult;
}
int main() {