Last remaining character after repeated removal of the first character and flipping of characters of a Binary String | C ++ program to implement the above approach ; Function to find the last removed character from the string ; Stores length of the string ; Base Case : ; If the second last character is '0' ; If the second last character is '1' ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; char lastRemovedCharacter ( string str ) { int n = str . length ( ) ; if ( n == 1 ) return str [ 0 ] ; if ( str [ n - 2 ] == '0' ) { return ( '1' - str [ n - 1 ] + '0' ) ; } else return str [ n - 1 ] ; } int main ( ) { string str = "10010" ; cout << lastRemovedCharacter ( str ) ; }