Skip to content

Commit

Permalink
Merge pull request #87 from Mayankrsmk/main
Browse files Browse the repository at this point in the history
Added Day 18 solution in Java
  • Loading branch information
arya2004 authored Oct 18, 2024
2 parents 8615e83 + 4f0eb89 commit 2c4b795
Showing 1 changed file with 31 additions and 0 deletions.
31 changes: 31 additions & 0 deletions solutions/day18/solution_java.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package solutions.day18;

class Solution {
public int countMaxOrSubsets(int[] nums) {
int maxOr = 0;
for(int x : nums){
maxOr |= x;
}

int currOr = 0;

return count(0,nums,currOr,maxOr);
}

public int count(int idx, int[] nums, int currOr, int maxOr){

if(idx==nums.length){
if(currOr==maxOr){
return 1;
}
return 0;
}


int take = count(idx+1, nums, currOr | nums[idx], maxOr);

int notTake = count(idx+1, nums, currOr, maxOr);

return take + notTake;
}
}

0 comments on commit 2c4b795

Please sign in to comment.