15,936,294 members
1.00/5 (1 vote)
See more:
```int findSubArray(int arr[], int n)
{
int sum = 0 ;
int maxsize = -1 , startindex = 0 ;
int endindex = 0 ;

for (int i = 0; i < n - 1; i++)
{
sum = (arr[i] == 0) ? -1 : 1 ;

for ( int j = i + 1 ; j < n ; j++ )
{
if ( arr[j] == 0 )
{
sum += -1;
}
else
{
sum += 1;
}

if ( sum == 0 && maxsize < j - i + 1 )
{

maxsize = j - i + 1;
startindex = i;

}

}

}

endindex = startindex + maxsize - 1 ;

if ( maxsize == -1 )
{
System.out.println( "No such subarray" );
}

else
{
System.out.println( startindex + " to " + endindex );
}

return maxsize ;

}
```

What I have tried:

I am not able to completely understand how this code works for finding the largest subarray with equal number of 0s and 1s . I am looking for crystal clear explanation . Please help .
Posted
Updated 7-Feb-18 10:26am

## Solution 2

There is a tool that allow you to see what your code is doing, its name is debugger. It is also a great learning tool because it show you reality and you can see which expectation match reality.
When you don't understand what your code is doing or why it does what it does, the answer is debugger.
Use the debugger to see what your code is doing. Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.

Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]
http://docs.oracle.com/javase/7/docs/technotes/tools/windows/jdb.html[^]
https://www.jetbrains.com/idea/help/debugging-your-first-java-application.html[^]
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't find bugs, it just help you to. When the code don't do what is expected, you are close to a bug.