Largest subarray with 0 and 1 is passing but it is lengthy

Code link – https://ide.codingblocks.com/s/35315
Question link – https://hack.codingblocks.com/contests/c/509/1302
My code is passing the test cases but I want to know if there is some technique to make this easy. Actually, I want to know that if this can be solved easily without doing so much stuff which I did and now it looks like a mess :sweat_smile: So, if u have any easy way to achieve this then plz share :grinning:

A very easy approach to this question is that
you can consider all 0 as -1
then you only need to find the largest subarray whose sum is 0.
This can be easily done by traversing a loop once only.

1 Like

Unable to figure out how to traverse the loop once only to find largest subarray .

Using Kadane’s algorithm
Its time complexity is O(n)