Last remaining character after repeated removal of the first character and flipping of characters of a Binary String | Java 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
import java . util . * ; class GFG { static char lastRemovedCharacter ( char [ ] str ) { int n = str . length ; if ( n == 1 ) return str [ 0 ] ; if ( str [ n - 2 ] == '0' ) { return ( char ) ( '1' - str [ n - 1 ] + '0' ) ; } else return str [ n - 1 ] ; } public static void main ( String [ ] args ) { String str = "10010" ; System . out . print ( lastRemovedCharacter ( str . toCharArray ( ) ) ) ; } }