# Longest subsequence with no 0 after 1

Given a binary array, find length of the longest subsequence such that there is no 0 after a 1.

Examples:

```Input : 1 1 0 1
Output : 3
Explanation :
If we remove 0 from the array, then no
zero comes right after one (satisfying
the condition) and the maximum game
left are 3 (i.e. 1 1 1)

Input : 0
Output : 1
Explanation :
Since he wants to save maximum game in
the array. He doesn't remove any game.
```

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

Let’s find out how many zeros will be in this sequence and then take all ones which come after the last zero. On each step take the next zero from the beginning of the sequence and count ones after it. Update answer with the maximum value.

You can pre-calculate number of ones on suffix.
E.g. 0 1 0 0 1 1 1

After calculating the suffix the array becomes :
0 4 0 0 3 2 1

Move from start to end and each time zero is found in the array increment numberofzeros by 1. If the array[index] not zero then res = max(res, numberofzeros + value of array at that index).
And then after the loop : res = max(res, numberofzeros)

 `// CPP program to find longest subsequence ` `// such that there is no 0 after 1. ` `#include ` `using` `namespace` `std; ` ` `  `int` `maxSubseq(``int` `vec[], ``int` `n) {     ` `         `  `    ``// Store the count of number of ones ` `    ``// from right to left in the array ` `    ``int` `suffix = 0; ` `    ``for` `(``int` `i = n - 1; i >= 0; i--) ` `    ``{ ` `        ``if` `(vec[i] == 1) ` `        ``{ ` `            ``suffix++; ` `            ``vec[i] = suffix; ` `        ``} ` `    ``} ` `     `  `    ``// Traverse from left to right, keep count ` `    ``// of 0s and for every 0, check number of  ` `    ``// 1s after it. Update the result if needed. ` `    ``int` `res = 0; ` `    ``int` `zero = 0;     ` `    ``for` `(``int` `i = 0; i < n; i++) ` `    ``{ ` `        ``if` `(vec[i] == 0) ` `            ``zero++; ` `     `  `        ``// Checking the maximum size ` `        ``if` `(vec[i] > 0) ` `            ``res = max(res, zero + vec[i]); ` `    ``} ` `     `  `    ``// Checking the maximum size ` `    ``return` `max(res, zero); ` `} ` ` `  `// Driver Code ` `int` `main() ` `{ ` `    ``int` `input[] = { 0, 1, 0, 0, 1, 0 }; ` `    ``int` `n = ``sizeof``(input) / ``sizeof``(input[0]);     ` `    ``cout << maxSubseq(input, n); ` `    ``return` `0; ` `} `

 `// java program to find longest subsequence ` `// such that there is no 0 after 1. ` `import` `java.io.*; ` ` `  `public` `class` `GFG { ` ` `  `    ``static` `int` `maxSubseq(``int` `[]vec, ``int` `n) ` `    ``{  ` `             `  `        ``// Store the count of number of ` `        ``// ones from right to left in ` `        ``// the array ` `        ``int` `suffix = ``0``; ` `         `  `        ``for` `(``int` `i = n - ``1``; i >= ``0``; i--) ` `        ``{ ` `            ``if` `(vec[i] == ``1``) ` `            ``{ ` `                ``suffix++; ` `                ``vec[i] = suffix; ` `            ``} ` `        ``} ` `         `  `        ``// Traverse from left to right, keep ` `        ``// count of 0s and for every 0, check ` `        ``// number of 1s after it. Update the ` `        ``// result if needed. ` `        ``int` `res = ``0``; ` `        ``int` `zero = ``0``;  ` `         `  `        ``for` `(``int` `i = ``0``; i < n; i++) ` `        ``{ ` `            ``if` `(vec[i] == ``0``) ` `                ``zero++; ` `         `  `            ``// Checking the maximum size ` `            ``if` `(vec[i] > ``0``) ` `                ``res = Math.max(res, zero + vec[i]); ` `        ``} ` `         `  `        ``// Checking the maximum size ` `        ``return` `Math.max(res, zero); ` `    ``} ` `     `  `    ``// Driver Code ` `    ``static` `public` `void` `main (String[] args) ` `    ``{ ` `         `  `        ``int` `[]input = { ``0``, ``1``, ``0``, ``0``, ``1``, ``0` `}; ` `        ``int` `n = input.length; ` `         `  `        ``System.out.println(maxSubseq(input, n)); ` `    ``} ` `} ` ` `  `// This code is contributed by vt_m. `

 `# Python 3 program to find longest subsequence ` `# such that there is no 0 after 1. ` ` `  `def` `maxSubseq(vec, n): ` `    ``# Store the count of number of ones ` `    ``# from right to left in the array ` `    ``suffix ``=` `0` `    ``i ``=` `n``-``1` `    ``while``(i >``=` `0``): ` `        ``if` `(vec[i] ``=``=` `1``): ` `            ``suffix ``+``=` `1` `            ``vec[i] ``=` `suffix ` `        ``i ``-``=` `1` `             `  `    ``# Traverse from left to right, keep count ` `    ``# of 0s and for every 0, check number of  ` `    ``# 1s after it. Update the result if needed. ` `    ``res ``=` `0` `    ``zero ``=` `0` `    ``for` `i ``in` `range``(``0``,n,``1``): ` `        ``if` `(vec[i] ``=``=` `0``): ` `            ``zero ``+``=` `1` `     `  `        ``# Checking the maximum size ` `        ``if` `(vec[i] > ``0``): ` `            ``res ``=` `max``(res, zero ``+` `vec[i]) ` `     `  `    ``# Checking the maximum size ` `    ``return` `max``(res, zero) ` ` `  `# Driver code ` ` ``if` `__name__ ``=``=` `'__main__'``: ` `    ``input` `=` `[``0``, ``1``, ``0``, ``0``, ``1``, ``0``]  ` `    ``n ``=` `len``(``input``)  ` `    ``print``(maxSubseq(``input``, n)) ` ` `  `# This code is contributed by  ` `# Surendra_Gangwar `

 `// C# program to find longest subsequence ` `// such that there is no 0 after 1. ` `using` `System; ` ` `  `public` `class` `GFG { ` ` `  `    ``static` `int` `maxSubseq(``int` `[]vec, ``int` `n) ` `    ``{  ` `             `  `        ``// Store the count of number of ` `        ``// ones from right to left in ` `        ``// the array ` `        ``int` `suffix = 0; ` `         `  `        ``for` `(``int` `i = n - 1; i >= 0; i--) ` `        ``{ ` `            ``if` `(vec[i] == 1) ` `            ``{ ` `                ``suffix++; ` `                ``vec[i] = suffix; ` `            ``} ` `        ``} ` `         `  `        ``// Traverse from left to right, keep ` `        ``// count of 0s and for every 0, check ` `        ``// number of 1s after it. Update the ` `        ``// result if needed. ` `        ``int` `res = 0; ` `        ``int` `zero = 0;  ` `         `  `        ``for` `(``int` `i = 0; i < n; i++) ` `        ``{ ` `            ``if` `(vec[i] == 0) ` `                ``zero++; ` `         `  `            ``// Checking the maximum size ` `            ``if` `(vec[i] > 0) ` `                ``res = Math.Max(res, zero + vec[i]); ` `        ``} ` `         `  `        ``// Checking the maximum size ` `        ``return` `Math.Max(res, zero); ` `    ``} ` `     `  `    ``// Driver Code ` ` `  `    ``static` `public` `void` `Main () ` `    ``{ ` `         `  `        ``int` `[]input = { 0, 1, 0, 0, 1, 0 }; ` `        ``int` `n = input.Length; ` `         `  `        ``Console.WriteLine(maxSubseq(input, n)); ` `    ``} ` `} ` ` `  `// This code is contributed by vt_m. `

 `= 0; ``\$i``--) ` `    ``{ ` `        ``if` `(``\$vec``[``\$i``] == 1) ` `        ``{ ` `            ``\$suffix``++; ` `            ``\$vec``[``\$i``] = ``\$suffix``; ` `        ``} ` `    ``} ` `     `  `    ``// Traverse from left to  ` `    ``// right, keep count of  ` `    ``// 0s and for every 0,  ` `    ``// check number of 1s after ` `    ``// it. Update the result if ` `    ``// needed. ` `    ``\$res` `= 0; ` `    ``\$zero` `= 0;  ` `    ``for` `(``\$i` `= 0; ``\$i` `< ``\$n``; ``\$i``++) ` `    ``{ ` `        ``if` `(``\$vec``[``\$i``] == 0) ` `            ``\$zero``++; ` `     `  `        ``// Checking the ` `        ``// maximum size ` `        ``if` `(``\$vec``[``\$i``] > 0) ` `            ``\$res` `= max(``\$res``, ``\$zero` `+  ` `                             ``\$vec``[``\$i``]); ` `    ``} ` `     `  `    ``// Checking the ` `    ``// maximum size ` `    ``return` `max(``\$res``, ``\$zero``); ` `} ` ` `  `// Driver Code ` `\$input` `= ``array``(0, 1, 0, 0, 1, 0); ` `\$n` `= ``count``(``\$input``);  ` `echo` `maxSubseq(``\$input``, ``\$n``); ` ` `  `// This code is contributed ` `// by anuj_67. ` `?> `

Output:
```4
```

This article is contributed by Sachin Bisht. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.