Skip to content

Commit

Permalink
Merge pull request #88 from prateek168/main
Browse files Browse the repository at this point in the history
Add Day 19 solution in C++
  • Loading branch information
arya2004 authored Oct 19, 2024
2 parents 2c4b795 + 92bc4e3 commit 5e8f539
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 0 deletions.
33 changes: 33 additions & 0 deletions solutions/day18/solution_cpp.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
class Solution
{
public:
void backtrack(const vector<int> &nums, int index, int currentOR, int maxOR, int &count)
{
if (currentOR == maxOR)
{
count++;
}

for (int i = index; i < nums.size(); ++i)
{
backtrack(nums, i + 1, currentOR | nums[i], maxOR, count);
}
}

int countMaxOrSubsets(vector<int> &nums)
{
int maxOR = 0;

// Step 1: Compute the maximum OR
for (int num : nums)
{
maxOR |= num;
}

int count = 0;
// Step 2: Backtrack to count the subsets
backtrack(nums, 0, 0, maxOR, count);

return count;
}
};
23 changes: 23 additions & 0 deletions solutions/day19/solution_cpp.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
class Solution {
public:
char findKthBit(int n, int k) {
if (n == 1) {
return '0';
}

int len = (1 << n) - 1;
int mid = len / 2 + 1;

if (k == mid) {
return '1';
}

if (k < mid) {
return findKthBit(n - 1, k);
}

char bit = findKthBit(n - 1, len - k + 1);
return bit == '0' ? '1' : '0';

}
};

0 comments on commit 5e8f539

Please sign in to comment.