Find max length odd parity substring

Given a binary string str, the task is to find the maximum length of the sub-string of str that has odd parity. A binary string is said be odd parity if it contains odd number of 1s.

Examples:

Input: str = “1001110”
Output: 6
“001110” is the valid sub-string.



Input: str = “101101”
Output: 5

Approach:

  1. Count the number of 1s in the given string and store it in a variable cnt.
  2. If cnt = 0 then there is no sub-string possible with odd parity so the result will be 0.
  3. If cnt is odd then the result will be the complete string.
  4. Now for the case when cnt is even and > 0, the required sub-string will either start at index 0 and end just before the last occurrence of 1 or start just after the first occurrence of 1 and end at the end of the given string.
  5. Choose the one with the greater length among the two sub-strings in the previous step.

Below is the implementation of the above approach:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ implementation of the approach 
#include <bits/stdc++.h>
using namespace std;
  
    // finds the index of character of string
    int indexOf(string s, char c, int i)
    {
        for(; i < s.length(); i++)
        if(s[i] == c)
        return i;
          
        return -1;
    }
      
    // finds the last index of character of string
    int lastIndexOf(string s,char c,int i)
    {
        for(; i >= 0; i--)
        if(s[i] == c)
            return i;
          
        return -1;
    }
  
    // Function to return the maximum 
    // length of the sub-string which 
    // contains odd number of 1s 
    int maxOddParity(string str, int n) 
    
  
        // Find the count of 1s in 
        // the given string 
        int cnt = 0; 
        for (int i = 0; i < n; i++) 
            if (str[i] == '1'
                cnt++; 
  
        // If there are only 0s in the string 
        if (cnt == 0) 
            return 0; 
  
        // If the count of 1s is odd then 
        // the complete string has odd parity 
        if (cnt % 2 == 1) 
            return n; 
  
        // Index of the first and the second 
        // occurrences of '1' in the string 
        int firstOcc = indexOf(str,'1',0); 
        int secondOcc = indexOf(str,'1', firstOcc + 1); 
  
        // Index of the last and the second last 
        // occurrences of '1' in the string 
        int lastOcc = lastIndexOf(str,'1',str.length()-1); 
        int secondLastOcc = lastIndexOf(str,'1', lastOcc - 1); 
  
        // Result will the sub-string ending just 
        // before the last occurrence of '1' or the 
        // sub-string starting just after the first 
        // occurrence of '1' 
        // choose the one with the maximum length 
        return max(lastOcc, n - firstOcc - 1); 
    
  
    // Driver code 
    int main() 
    
        string str = "101101"
        int n = str.length(); 
        cout<<(maxOddParity(str, n)); 
    
  
// This code is contributed by Arnab Kundu

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java implementation of the approach
public class GFG {
  
    // Function to return the maximum
    // length of the sub-string which
    // contains odd number of 1s
    static int maxOddParity(String str, int n)
    {
  
        // Find the count of 1s in
        // the given string
        int cnt = 0;
        for (int i = 0; i < n; i++)
            if (str.charAt(i) == '1')
                cnt++;
  
        // If there are only 0s in the string
        if (cnt == 0)
            return 0;
  
        // If the count of 1s is odd then
        // the complete string has odd parity
        if (cnt % 2 == 1)
            return n;
  
        // Index of the first and the second
        // occurrences of '1' in the string
        int firstOcc = str.indexOf('1');
        int secondOcc = str.indexOf('1', firstOcc + 1);
  
        // Index of the last and the second last
        // occurrences of '1' in the string
        int lastOcc = str.lastIndexOf('1');
        int secondLastOcc = str.lastIndexOf('1', lastOcc - 1);
  
        // Result will the sub-string ending just
        // before the last occurrence of '1' or the
        // sub-string starting just after the first
        // occurrence of '1'
        // choose the one with the maximum length
        return Math.max(lastOcc, n - firstOcc - 1);
    }
  
    // Driver code
    public static void main(String[] args)
    {
        String str = "101101";
        int n = str.length();
        System.out.print(maxOddParity(str, n));
    }
}

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 implementation of the approach
  
# Function to return the maximum
# length of the sub-string which
# contains odd number of 1s
def maxOddParity(string, n):
  
    # Find the count of 1s in
    # the given string
    cnt = 0
    for i in range(n):
        if string[i] != '1':
            cnt += 1
  
    # If there are only 0s in the string
    if cnt == 0:
        return 0
  
    # If the count of 1s is odd then
    # the complete string has odd parity
    if cnt % 2 == 1:
        return n
  
    # Index of the first and the second
    # occurrences of '1' in the string
    firstOcc = string.index('1')
    secondOcc = string.index('1', firstOcc + 1)
  
    # Index of the last and the second last
    # occurrences of '1' in the string
    lastOcc = string.rindex('1')
    secondLastOcc = string.rindex('1', 0, lastOcc)
  
    # Result will the sub-string ending just
    # before the last occurrence of '1' or the
    # sub-string starting just after the first
    # occurrence of '1'
    # choose the one with the maximum length
    return max(lastOcc, n - firstOcc - 1)
  
# Driver Code
if __name__ == "__main__":
    string = "101101"
    n = len(string)
    print(maxOddParity(string, n))
  
# This code is contributed by
# sanjeev2552

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# implementation of the above approach 
using System;
  
class GFG 
{
  
    // Function to return the maximum
    // length of the sub-string which
    // contains odd number of 1s
    static int maxOddParity(String str, int n)
    {
  
        // Find the count of 1s in
        // the given string
        int cnt = 0;
        for (int i = 0; i < n; i++)
            if (str[i] == '1')
                cnt++;
  
        // If there are only 0s in the string
        if (cnt == 0)
            return 0;
  
        // If the count of 1s is odd then
        // the complete string has odd parity
        if (cnt % 2 == 1)
            return n;
  
        // Index of the first and the second
        // occurrences of '1' in the string
        int firstOcc = str.IndexOf('1');
        int secondOcc = str.IndexOf('1', firstOcc + 1);
  
        // Index of the last and the second last
        // occurrences of '1' in the string
        int lastOcc = str.LastIndexOf('1');
        int secondLastOcc = str.LastIndexOf('1', lastOcc - 1);
  
        // Result will the sub-string ending just
        // before the last occurrence of '1' or the
        // sub-string starting just after the first
        // occurrence of '1'
        // choose the one with the maximum length
        return Math.Max(lastOcc, n - firstOcc - 1);
    }
  
    // Driver code
    public static void Main(String[] args)
    {
        String str = "101101";
        int n = str.Length;
        Console.WriteLine(maxOddParity(str, n));
    }
}
  
/* This code contributed by PrinciRaj1992 */

chevron_right


Output:

5


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.





Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.