Given a binary string **str** consisting of only 0’s and 1’s. The following two operations can be performed on it:

- One digit can delete another digit i.e. a 0 can delete a 1 and vice versa.
- If at any moment, the entire string consists only 0’s or 1’s, then the respective digit is printed.

The task is to print the remaining digit which will be left at the end.

**Examples:**

Input:str = “100”

Output:0

Explanation:

The 1st digit is 1 and it deletes the next digit 0.

The 2nd digit, i.e. 0, is deleted and now does not exists.

Now, the 3rd digit 0 deletes the 1st digit 1.

Since now only 0 is left, the output is 0.

Input:str = “10”

Output:1

**Approach:** For this queue data structure is used. The following steps can be followed to compute the answer:

- All the digits are added to the queue.
- Two counters are maintained as an array of size 2
**del[2]**which will represent the number of floating deletes present for each digit. - The queue is traversed until there exits at least one digit of both the types.
- Then for each digit in the queue if the
**delete**counter for this digit is not 0, then it is deleted. - Else, the
**delete**counter for the opposite digit is incremented and placed back into the queue.

Below is the implementation of the above approach:

## C++

`// C++ implementation of the above approach ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `string remainingDigit(string S, ` `int` `N) ` `{ ` ` ` ` ` ` ` `// Delete counters for each to ` ` ` `// count the deletes ` ` ` `int` `del[] = { 0, 0 }; ` ` ` ` ` `// Counters to keep track ` ` ` `// of characters left from each type ` ` ` `int` `count[] = { 0, 0 }; ` ` ` ` ` `// Queue to simulate the process ` ` ` `queue<` `int` `> q; ` ` ` ` ` `// Initializing the queue ` ` ` `for` `(` `int` `i = 0; i < N; i++) ` ` ` `{ ` ` ` `int` `x = S[i] == ` `'1'` `? 1 : 0; ` ` ` `count[x]++; ` ` ` `q.push(x); ` ` ` `} ` ` ` ` ` `// Looping till at least 1 digit is ` ` ` `// left from both the type ` ` ` `while` `(count[0] > 0 && count[1] > 0) ` ` ` `{ ` ` ` `int` `t = q.front(); ` ` ` `q.pop(); ` ` ` ` ` `// If there is a floating delete for ` ` ` `// current character we will ` ` ` `// delete it and move forward otherwise ` ` ` `// we will increase delete counter for ` ` ` `// opposite digit ` ` ` `if` `(del[t] > 0) ` ` ` `{ ` ` ` `del[t]--; ` ` ` `count[t]--; ` ` ` `} ` ` ` `else` ` ` `{ ` ` ` `del[t ^ 1]++; ` ` ` `q.push(t); ` ` ` `} ` ` ` `} ` ` ` ` ` `// If 0 are left ` ` ` `// then answer is 0 else ` ` ` `// answer is 1 ` ` ` `if` `(count[0] > 0) ` ` ` `return` `"0"` `; ` ` ` `return` `"1"` `; ` `} ` ` ` `// Driver Code ` `int` `main() ` `{ ` ` ` ` ` `// Input String ` ` ` `string S = ` `"1010100100000"` `; ` ` ` ` ` `// Length of String ` ` ` `int` `N = S.length(); ` ` ` ` ` `// Printing answer ` ` ` `cout << remainingDigit(S, N); ` `} ` ` ` `// This code is contributed by tufan_gupta2000 ` |

*chevron_right*

*filter_none*

## Java

`// Java implementation of the above approach ` ` ` `import` `java.util.*; ` ` ` `public` `class` `GfG { ` ` ` `private` `static` `String remainingDigit(String S, ` `int` `N) ` ` ` `{ ` ` ` `// Converting string to array ` ` ` `char` `c[] = S.toCharArray(); ` ` ` ` ` `// Delete counters for each to ` ` ` `// count the deletes ` ` ` `int` `del[] = { ` `0` `, ` `0` `}; ` ` ` ` ` `// Counters to keep track ` ` ` `// of characters left from each type ` ` ` `int` `count[] = { ` `0` `, ` `0` `}; ` ` ` ` ` `// Queue to simulate the process ` ` ` `Queue<Integer> q = ` `new` `LinkedList<>(); ` ` ` ` ` `// Initializing the queue ` ` ` `for` `(` `int` `i = ` `0` `; i < N; i++) { ` ` ` `int` `x = c[i] == ` `'1'` `? ` `1` `: ` `0` `; ` ` ` `count[x]++; ` ` ` `q.add(x); ` ` ` `} ` ` ` ` ` `// Looping till at least 1 digit is ` ` ` `// left from both the type ` ` ` `while` `(count[` `0` `] > ` `0` `&& count[` `1` `] > ` `0` `) { ` ` ` `int` `t = q.poll(); ` ` ` ` ` `// If there is a floating delete for ` ` ` `// current character we will ` ` ` `// delete it and move forward otherwise ` ` ` `// we will increase delete counter for ` ` ` `// opposite digit ` ` ` `if` `(del[t] > ` `0` `) { ` ` ` `del[t]--; ` ` ` `count[t]--; ` ` ` `} ` ` ` `else` `{ ` ` ` `del[t ^ ` `1` `]++; ` ` ` `q.add(t); ` ` ` `} ` ` ` `} ` ` ` ` ` `// If 0 are left ` ` ` `// then answer is 0 else ` ` ` `// answer is 1 ` ` ` `if` `(count[` `0` `] > ` `0` `) ` ` ` `return` `"0"` `; ` ` ` `return` `"1"` `; ` ` ` `} ` ` ` ` ` `// Driver Code ` ` ` `public` `static` `void` `main(String args[]) ` ` ` `{ ` ` ` ` ` `// Input String ` ` ` `String S = ` `"1010100100000"` `; ` ` ` ` ` `// Length of String ` ` ` `int` `N = S.length(); ` ` ` ` ` `// Printing answer ` ` ` `System.out.print(remainingDigit(S, N)); ` ` ` `} ` `} ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 implementation of the above approach ` `from` `collections ` `import` `deque; ` ` ` `def` `remainingDigit(S, N): ` ` ` ` ` `# Converting string to array ` ` ` `c ` `=` `[i ` `for` `i ` `in` `S] ` ` ` ` ` `# Delete counters for each to ` ` ` `# count the deletes ` ` ` `de ` `=` `[` `0` `, ` `0` `] ` ` ` ` ` `# Counters to keep track ` ` ` `# of characters left from each type ` ` ` `count ` `=` `[` `0` `, ` `0` `] ` ` ` ` ` `# Queue to simulate the process ` ` ` `q ` `=` `deque() ` ` ` ` ` `# Initializing the queue ` ` ` `for` `i ` `in` `c: ` ` ` `x ` `=` `0` ` ` `if` `i ` `=` `=` `'1'` `: ` ` ` `x ` `=` `1` ` ` `count[x] ` `+` `=` `1` ` ` `q.append(x) ` ` ` ` ` `# Looping till at least 1 digit is ` ` ` `# left from both the type ` ` ` `while` `(count[` `0` `] > ` `0` `and` `count[` `1` `] > ` `0` `): ` ` ` `t ` `=` `q.popleft() ` ` ` ` ` `# If there is a floating delete for ` ` ` `# current character we will ` ` ` `# delete it and move forward otherwise ` ` ` `# we will increase delete counter for ` ` ` `# opposite digit ` ` ` `if` `(de[t] > ` `0` `): ` ` ` `de[t] ` `-` `=` `1` ` ` `count[t] ` `-` `=` `1` ` ` `else` `: ` ` ` `de[t ^ ` `1` `] ` `+` `=` `1` ` ` `q.append(t) ` ` ` ` ` `# If 0 are left ` ` ` `# then answer is 0 else ` ` ` `# answer is 1 ` ` ` `if` `(count[` `0` `] > ` `0` `): ` ` ` `return` `"0"` ` ` `return` `"1"` ` ` `# Driver Code ` `if` `__name__ ` `=` `=` `'__main__'` `: ` ` ` ` ` `# Input String ` ` ` `S ` `=` `"1010100100000"` ` ` ` ` `# Length of String ` ` ` `N ` `=` `len` `(S) ` ` ` ` ` `# Printing answer ` ` ` `print` `(remainingDigit(S, N)) ` ` ` `# This code is contributed by mohit kumar 29 ` |

*chevron_right*

*filter_none*

## C#

`// C# implementation of the above approach ` `using` `System; ` `using` `System.Collections.Generic; ` ` ` `public` `class` `GfG ` `{ ` ` ` `private` `static` `String remainingDigit(String S, ` `int` `N) ` ` ` `{ ` ` ` `// Converting string to array ` ` ` `char` `[]c = S.ToCharArray(); ` ` ` ` ` `// Delete counters for each to ` ` ` `// count the deletes ` ` ` `int` `[]del = { 0, 0 }; ` ` ` ` ` `// Counters to keep track ` ` ` `// of characters left from each type ` ` ` `int` `[]count = { 0, 0 }; ` ` ` ` ` `// Queue to simulate the process ` ` ` `List<` `int` `> q = ` `new` `List<` `int` `>(); ` ` ` ` ` `// Initializing the queue ` ` ` `for` `(` `int` `i = 0; i < N; i++) ` ` ` `{ ` ` ` `int` `x = c[i] == ` `'1'` `? 1 : 0; ` ` ` `count[x]++; ` ` ` `q.Add(x); ` ` ` `} ` ` ` ` ` `// Looping till at least 1 digit is ` ` ` `// left from both the type ` ` ` `while` `(count[0] > 0 && count[1] > 0) ` ` ` `{ ` ` ` `int` `t = q[0]; ` ` ` `q.RemoveAt(0); ` ` ` ` ` `// If there is a floating delete for ` ` ` `// current character we will ` ` ` `// delete it and move forward otherwise ` ` ` `// we will increase delete counter for ` ` ` `// opposite digit ` ` ` `if` `(del[t] > 0) ` ` ` `{ ` ` ` `del[t]--; ` ` ` `count[t]--; ` ` ` `} ` ` ` `else` ` ` `{ ` ` ` `del[t ^ 1]++; ` ` ` `q.Add(t); ` ` ` `} ` ` ` `} ` ` ` ` ` `// If 0 are left ` ` ` `// then answer is 0 else ` ` ` `// answer is 1 ` ` ` `if` `(count[0] > 0) ` ` ` `return` `"0"` `; ` ` ` `return` `"1"` `; ` ` ` `} ` ` ` ` ` `// Driver Code ` ` ` `public` `static` `void` `Main(String []args) ` ` ` `{ ` ` ` ` ` `// Input String ` ` ` `String S = ` `"1010100100000"` `; ` ` ` ` ` `// Length of String ` ` ` `int` `N = S.Length; ` ` ` ` ` `// Printing answer ` ` ` `Console.Write(remainingDigit(S, N)); ` ` ` `} ` `} ` ` ` `// This code is contributed by Rajput-Ji ` |

*chevron_right*

*filter_none*

**Output:**

0

**Time Complexity:** O(N)

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Change the given string according to the given conditions
- Expand the string according to the given conditions
- Find maximum Subsequence Sum according to given conditions
- Split the array into equal sum parts according to given conditions
- Count minimum character replacements required such that given string satisfies the given conditions
- Queries to find the last non-repeating character in the sub-string of a given string
- Find last two remaining elements after removing median of any 3 consecutive elements repeatedly
- Find last remaining element after reducing the Array
- Find the last remaining element after repeated removal of odd and even indexed elements alternately
- Find the player who is the last to remove any character from the beginning of a Binary String
- Given a string and an integer k, find the kth sub-string when all the sub-strings are sorted according to the given condition
- Remove duplicates from string keeping the order according to last occurrences
- Last element remaining by deleting two largest elements and replacing by their absolute difference if they are unequal
- Maximize the last Array element as per the given conditions
- Find the last non repeating character in string
- How to find the first and last character of a string in Java
- Find last index of a character in a string
- Remove the first and last occurrence of a given Character from a String
- Check if frequency of character in one string is a factor or multiple of frequency of same character in other string
- Find a string such that every character is lexicographically greater than its immediate next character

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.