Skip to content

Create 3480. Maximize Subarrays After Removing One Conflicting Pair1 #850

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 29, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions 3480. Maximize Subarrays After Removing One Conflicting Pair1
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
class Solution
{
public:
int countMaxOrSubsets(vector<int>& nums)
{
// Step 2: Compute the maximum possible OR of all numbers
int maxOr = 0;
for (int num : nums)
{
maxOr |= num;
}

int count = 0;

// Step 3: Start the recursive backtracking
backtrack(nums, 0, 0, maxOr, count);

// Step 6: Return the final result
return count;
}

// Step 3: Recursive function to explore subsets
void backtrack(vector<int>& nums, int index, int currentOr, int maxOr, int& count)
{
// Step 4: Base case – all numbers considered
if (index == nums.size())
{
// Step 4: Check if current OR equals the max OR
if (currentOr == maxOr)
{
count++; // Valid subset found
}

return;
}

// Step 5: Recursive calls
// Include current number
backtrack(nums, index + 1, currentOr | nums[index], maxOr, count);

// Exclude current number
backtrack(nums, index + 1, currentOr, maxOr, count);
}
};
Loading