From e9cbc9fcbdc2ac763e9a8c0ddbbf6fbdba134079 Mon Sep 17 00:00:00 2001 From: Tahanima Chowdhury Date: Sun, 10 Oct 2021 23:07:30 +0600 Subject: [PATCH] Add LeetCode 1863 java, cpp, cs --- .../1863_SumOfAllSubsetXorTotals/Solution.cpp | 19 +++++++++++++++++++ .../1863_SumOfAllSubsetXorTotals/Solution.cs | 18 ++++++++++++++++++ .../Solution.java | 18 ++++++++++++++++++ 3 files changed, 55 insertions(+) create mode 100644 Algorithms/Easy/1863_SumOfAllSubsetXorTotals/Solution.cpp create mode 100644 Algorithms/Easy/1863_SumOfAllSubsetXorTotals/Solution.cs create mode 100644 Algorithms/Easy/1863_SumOfAllSubsetXorTotals/Solution.java diff --git a/Algorithms/Easy/1863_SumOfAllSubsetXorTotals/Solution.cpp b/Algorithms/Easy/1863_SumOfAllSubsetXorTotals/Solution.cpp new file mode 100644 index 0000000..ac3031d --- /dev/null +++ b/Algorithms/Easy/1863_SumOfAllSubsetXorTotals/Solution.cpp @@ -0,0 +1,19 @@ +class Solution { +public: + int subsetXORSum(vector& nums) { + int n = nums.size(), sum = 0; + int limit = (1 << n) - 1; + + for (int mask = 1; mask <= limit; mask++) { + int _xor = 0; + for (int pos = 0; pos < n; pos++) { + if (mask & (1 << pos)) { + _xor ^= nums[pos]; + } + } + sum += _xor; + } + + return sum; + } +}; \ No newline at end of file diff --git a/Algorithms/Easy/1863_SumOfAllSubsetXorTotals/Solution.cs b/Algorithms/Easy/1863_SumOfAllSubsetXorTotals/Solution.cs new file mode 100644 index 0000000..f05d412 --- /dev/null +++ b/Algorithms/Easy/1863_SumOfAllSubsetXorTotals/Solution.cs @@ -0,0 +1,18 @@ +public class Solution { + public int SubsetXORSum(int[] nums) { + int n = nums.Length, sum = 0; + int limit = (1 << n) - 1; + + for (int mask = 1; mask <= limit; mask++) { + int _xor = 0; + for (int pos = 0; pos < n; pos++) { + if ((mask & (1 << pos)) > 0) { + _xor ^= nums[pos]; + } + } + sum += _xor; + } + + return sum; + } +} \ No newline at end of file diff --git a/Algorithms/Easy/1863_SumOfAllSubsetXorTotals/Solution.java b/Algorithms/Easy/1863_SumOfAllSubsetXorTotals/Solution.java new file mode 100644 index 0000000..29b1c9d --- /dev/null +++ b/Algorithms/Easy/1863_SumOfAllSubsetXorTotals/Solution.java @@ -0,0 +1,18 @@ +class Solution { + public int subsetXORSum(int[] nums) { + int n = nums.length, sum = 0; + int limit = (1 << n) - 1; + + for (int mask = 1; mask <= limit; mask++) { + int _xor = 0; + for (int pos = 0; pos < n; pos++) { + if ((mask & (1 << pos)) > 0) { + _xor ^= nums[pos]; + } + } + sum += _xor; + } + + return sum; + } +} \ No newline at end of file