Skip to content

Commit

Permalink
feat: LC-2172: Maximum AND Sum of Array
Browse files Browse the repository at this point in the history
add java solution for leetcode problem 2172 - Maximum AND Sum of Array
  • Loading branch information
galaumang committed Oct 5, 2023
1 parent 2206560 commit dc2d804
Showing 1 changed file with 25 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package dsalgo.array;

import java.util.Arrays;

public class LC2172_MaximumANDSumArray {
public int maximumANDSum(int[] nums, int numSlots) {
int n = 2 * numSlots;
int nSelected = 1 << n;

int[] dp = new int[nSelected];
int[] nos = Arrays.copyOf(nums, n);

for(int mask = 1; mask < nSelected; ++mask) {
int selected = Integer.bitCount(mask);
int slot = (selected + 1)/2;

for ( int i = 0; i < n; ++i) {
if ((mask >> i & 1) == 1) {
dp[mask] = Math.max(dp[mask],dp[mask ^ 1 << i]+(slot & nos[i]));
}
}
}
return dp[nSelected - 1];
}
}

0 comments on commit dc2d804

Please sign in to comment.