15,919,178 members
See more:
Find largest alternating 1s and 0s subsequence in a string containing only 1s and 0s.also find the starting index for the largest subsequence

Example for 1101011 rhe longest alternating subsequence length is 5 from index 1 to 5.

What I have tried:

Tried doing it by comparing consecutive elements and if they are not equal checking current length with max size

Java
```int findSubArray(int arr[], int n)
{
int sum = 0;
int maxsize = -1, startindex = 0;
int endindex = 0;

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

endindex = startindex+maxsize-1;
if (maxsize == -1)
System.out.println("No such subarray");
else
System.out.println(startindex+" to "+endindex);

return maxsize;
}```

int [] ia = {1, 1, 0, 1, 0, 1, 1}
findSubArray (ia, 7);
Returns: 5 and prints 0 to 4

The problem is that although this prints the length correctly which is 5, the indexes are incorrect. Ideal output should be 1 to 5.

To rectify this, if I do j = i + 1, then the whole matching goes for a toss and I get the indexes as 0 to 0.

What is the mistake in the above code? Also, any pseudocode for alternative approach would help?
Posted
Updated 15-Feb-18 5:12am
v2
OriginalGriff 15-Feb-18 3:11am
And?
What does it do that you didn't expect, or not do that you did?
What data did you feed it?
What results did you get?
What have you tried in order to find out why it does that?
What did the debugger show was happening?

## Solution 1

I would write:
Java
```static int findSubArray(int arr[])
{
int max_size = -1, start_index = 0;
int end_index = 0;
int start_sequence=0;
boolean in_sequence = false;

int n = arr.length;

for (int i = 0; i < n - 1; i++)
{
if ( in_sequence )
{
if ( arr[i] == arr[i+1])
{
in_sequence = false;
if ( i - start_sequence >= max_size)
{
max_size = i -start_sequence + 1;
start_index = start_sequence;
}
}
}
else
{
if ( arr[i] != arr[i+1] )
{
in_sequence = true;
start_sequence = i;
}
}
}
if ( in_sequence )
{
if ( n - start_sequence > max_size)
{
max_size = n -start_sequence;
start_index = start_sequence;
}
}

end_index = start_index + max_size - 1;
if (max_size == -1)
System.out.println("No such subarray");
else
System.out.println(start_index + " to " + end_index);

return max_size;
}
```

## Solution 2

When you don't understand what you code is doing, the answer is debugger.
this line look suspicious
Java
`if (arr[i] != arr[i+1] && maxsize < i - j + 1)`

it prevent from finding the second sequence correctly in:
Java
`int [] ia = {1, 1, 0, 1, 0, 1, 1, 0, 1, 0, 1, 0, 1, 0, 1}`

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.