I was solving this problem. While solving this problem i encountered a sub-problem.
We have an array of n length and we want to divide it into three parts such that after dividing the array sum of individual arrays should be as close to the power of 2 greater than or equal to the sum as possible.
More formally, let say sum1, sum2, sum3 are the sums of subarrays. Our answer is minimum possible value of (p1 — sum1) + (p2 — sum2) + (p3 — sum3). Here, p1, p2, p3 are the powers of two.
For Example : let say our array is [4, 1, 1, 2, 3, 1]
-> If we divide this array as [4, 1], [1, 2], [3, 1] then our answer is (8 — 5) + (4 — 3) + (4 — 4) = 0.
-> If we divide this array as [4], [1, 1, 2], [3, 1] the our answer is (4 — 4) + (4 — 4) + (4 — 4) = 0 which is optimal in this case.
Constraints: 1<= n <= 1e5, 1 <= ai <=n
I would be grateful if you help me to solve this Problem.