Last Updated: 7 Apr, 2022

Maximum AND Sum of Array

Hard
Asked in companies
HCL TechnologiesWestern DigitalGoogle inc

Problem statement

You are given an array of coins ‘COINS’ of length ‘N’ and there are ‘S’ number of slots numbered from 1 to S such that 2*S >= N.

You have to place all N coins into some slots so that no slot contains more than two coins. After placing the coins you will calculate the AND sum as the sum of all the values obtained by performing the bitwise AND operation between the slot number and the value of the coin placed in that slot number .

You have to find AND sum between the coins and the slots.

Input Format :
The first line of the input contains a single integer 'T', representing the number of test cases.

The first line of each test case contains two integers ‘N’ and ‘S’ denoting the number of coins and the number of slots respectively.

The second line contains an integer array denoting the value of coins.
Example :
Number of coins, N = 9
Number of slots, S = 8
Coins array, COINS = [14, 7, 9, 8, 2, 4, 1, 1, 1, 9]

One possible placement is to put coins having value:
[14, 7] into slot number 7, 
[9, 8] into slot number 8,
[2] into slot number 2,
[4] into slot number 4,
[1, 1] into slot number 3,
[1, 9] into slot number 1.

This gives the maximum AND sum of :
(14 AND 7) + (7 AND 7) + (9 AND 8) + (8 AND 8) + (2 AND 2) + (4 AND 3) + (11 AND 3) + (1 AND 9) = 6 + 7 + 8 + 8 + 2 + 4 + 3 + 1 + 1 = 40.
Note that slots number 5, 6 are empty which is permitted.
Output format :
For each test case, output a single integer , AND sum between the coins and the slots.

Print the output of each test case in a new line.
Note :
You don’t need to print anything. It has already been taken care of. Just implement the given function.
Constraints :
1 <= T <= 10 
1 <= S <= 9
1 <= N <= 2 * S
1 <= COINS[i] <= 15

Time Limit: 5 sec

Approaches

01 Approach

  1. As the number of slots is very low this hints us to bitmasking where each mask will represent the state of all slots.
  2. We will create a mask of base 3, wherein each mask:
    • Each digit will represent a slot.
    • The value of each digit (0, 1, 2) will represent the number of coins in that slot.
  3. For this we will create a 2D DP in which :
    • The 1st dimension will represent each coin.
    • The 2nd dimension will represent the state of all slots for that coin.
  4. The transitions will be like this:
    • DP[coin][mask] = ( maximum of DP[coin][mask] and Dp[previous coin][previous state of kth slot in mask] ) + bitwise AND of ith coin and kth slot.
  5. The answer would be the maximum of all slots values in the last coin.


 

The steps are as follows : 

  1. Create a 2D DP array of size N x 3^S.
  2. Initialize the DP array as :
    • For each coin i from 0 to N-1
      • For each slot j from 0 to  S-1 set the DP[i][j] as bitwise AND of ith coin and mask of jth slot(3^j).
  3. To calculate the DP values :
    • For each coin i from 0 to N-1
      • For each mask from 0 to 3^S-1, we calculate the max value of this mask as :
        • For each slot k from 0 to S-1, first, we check if the kth slot is mask is valid i.e if the kth digit of the mask is not zero as we if we put a coin in the kth slot then the kth digit of mask increases by one but to calculate the maximum AND sum up to this coin we look for the value of kth digit of mask 1 less than the current value of kth digit of the mask.
        • If true then : DP[i][mask] = max(DP[i][mask], DP[i-1][mask-3^k]+coin[i] AND (k+1)).
  4. Create an ans variable to store answers from the maximum of all values in array DP[N-1].