InterviewBit Academy is now Scaler!
InterviewBit Academy is now Scaler Academy!

DIVISIONS

Given an array of integers A of size N and an integer B.

Power of a subarray = (Number of set bits in all elements of subarray) X (size of subarray).

Divide array A in atmost B subarrays such that sum of power of all subarrays is minimum.

Return minimum possible sum of power of all subarrays.



Input Format

The first argument given is the integer array A.
The second argument given is the integer B.

Output Format

Return minimum possible sum of power of all subarrays.

Constraints

1 <= N <= 1000
0 <= A[i] <= 100000
1 <= B <= 100

For Example

Example Input 1:
    A = [11, 11, 11, 24]
    B = 2
Example Output 1:
    22
Example Explanation 1:
    11 = 01011 (set bits = 3)
    24 = 11000 (set bits = 2)
    
    Optimal division = [11, 11], [11, 24]
    size of subarray 1 = 2
    size of subarray 2 = 2
    power = (size of subarray 1) * (set bits in subarray 1) + (size of subarray 2) * (set bits in subarray 2)
    
    power = 2 * (3 + 3) + 2 * (3 + 2) = 22
Input 2:
    A = [8, 3, 5, 1]
    B = 1
Output 2:
    24
Explanation 2:
    Only possible division = [8, 3, 5, 1]
    8 = 1000 (set bits = 1)
    3 = 0011 (set bits = 2)
    5 = 0101 (set bits = 2)
    1 = 0001 (set bits = 1)
    
    Number of set bits = 6
    Size of subarray = 4
    
    Power = 6 * 4 = 24
NOTE: You only need to implement the given function. Do not read input, instead use the arguments to the function. Do not print the output, instead return values as specified. Still have a doubt? Checkout Sample Codes for more details.
Start solving DIVISIONS on Interview Code Editor
Sign Up
to access hints and editorial solutions for DIVISIONS

Discussion


Loading...
Click here to start solving coding interview questions