- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Find max length odd parity substring
Introduction
In this tutorial, we develop an approach to finding the maximum length odd parity substring. Odd parity in a substring means the number of times 1 repeats in a string is odd. Parity in C++ defines the bit set number and is 1s in a number. There are two types of parity: even and odd.
When the total number of “1” in a binary representation is odd it is known as an odd parity string. In this tutorial, we find the maximum length odd parity substring using C++ programming concepts.
Implementation 1
String = 101100
Output = 6
In the above example, the length of the maximum odd parity substring is 6 and that substring can be 011100. In this substring, the total number of 1s is 3, which is an odd number. Making it an odd parity substring.
Implementation 2
String = 1011010
Output = 6
In the above example, the largest length odd parity substring from a given string is 6. The possible substring can be 011010 as it contains a total of 3 "1", making it an odd parity substring.
Algorithm
Create a counter variable ct to count 1s in the input string.
If the value of ct = 0 then no odd parity substring can be formed as the input string contains only 0.
If the total number of 1s in the input string is odd, the length of the substring is equal to the length of the string.
When the value of the ct variable is even, then the substring can be formed by two possibilities.
Find the longest odd parity substring.
Print the length.
Example
We implement example 2 using C++ and use the length() function of the string class to find the length of the input string and the generated substring.
#include <bits/stdc++.h> using namespace std; // user defined function for calculating the index value of string int indexOfString(string st, char ch, int j){ for(; j < st.length(); j++) if(st[j] == ch) return j; return -1; } //finding the lsat index value of the string int lastIndexOfString(string st,char ch,int j){ for(; j >= 0; j--) if(st[j] == ch) return j; return -1; } //user defined function to find the length of the longest odd parity substring int maxSubstring(string s, int l){ //variable for counting 1s int ct = 0; for (int j = 0; j < l; j++) if (s[j] == '1') ct++; //different counter variable conditions if (ct == 0) return 0; if (ct % 2 == 1) return l; int firstTime = indexOfString(s,'1',0); int secondTime = indexOfString(s,'1', firstTime + 1); int lastTime = lastIndexOfString(s,'1',s.length()-1); int secondLastTime = lastIndexOfString(s,'1', lastTime - 1); return max(lastTime, l - firstTime - 1); } // Controller int main(){ string s = "1011010"; int l = s.length(); cout<<"The maximum length of the odd parity substring is:" <<(maxSubstring(s, l)); }
Output
The maximum length of the odd parity substring is: 6
Conclusion
In this tutorial, we developed an approach to find the length of the longest odd parity substring from the given input string. The length of the odd parity substring is calculated using a counter variable and defining different if conditions for it.
We used the length() function of the string class that helps find the length of the substring and the index values of the input string. The index values generate the substring.