question: flip bits
Please explain the logic behind the question
hello @aarijrab
A simple solution is to consider all subarrays and find a subarray with maximum value of (count of 0s) – (count of 1s) . Let this value be max_diff. Finally return count of zeros in original array plus max_diff.
time complexity will be O(n^2).
another approach->
The idea is to consider every 0 as 1 and every 1 as -1, find the sum of largest subarray sum in this modified array. This sum is our required max_diff ( count of 0s – count of 1s in any subarray). Finally we return the max_diff plus count of ones in original array.
time complexity O(n)
I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.
On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.