Given an n-bit vector and an integer k, 1 <= k <= n, we have to maximize the number of ones in it by applying the following operation any number of times(including zero):
- Choose exactly k bits(not necessarily continuous) and flip their state (0 to 1, 1 to 0);
After some analysis, I have come to the conclusion if n > k, we can also flip any two bits simultaneously. For example for n = 5, k = 4. We can do something like this to flip last two bits only.
- xxxx_
- xxx_x
'x' represents that we flip the bit at that position.
But I am not sure how to proceed after that and I am unable to make any more observations. So, what would be a correct approach? You may assume an n^2 algorithm is feasible.