Minimum integer that can be obtained by swapping adjacent digits of different parity | Java implementation of the above approach . ; Function to return the minimum number ; Store the elements which are divisible by two in stack1 ; Store the elements which are not divisible by two in stack2 . ; Concatenate the answer with smaller value of the topmost elements of both the stacks and then pop that element ; Concatenate the answer with remaining values of stack1 . ; Concatenate the answer with remaining values of stack2 . ; Driver code ; Function calling
import java . util . * ; class GFG { static int minimumNo ( int n ) { int ans = 0 ; Stack < Integer > stack1 = new Stack < Integer > ( ) ; Stack < Integer > stack2 = new Stack < Integer > ( ) ; while ( n != 0 ) { int r = n % 10 ; if ( r % 2 == 0 ) { stack1 . add ( r ) ; } else { stack2 . add ( r ) ; } n = n / 10 ; } while ( ! stack1 . isEmpty ( ) && ! stack2 . isEmpty ( ) ) { if ( stack1 . peek ( ) < stack2 . peek ( ) ) { ans = ans * 10 + stack1 . peek ( ) ; stack1 . pop ( ) ; } else { ans = ans * 10 + stack2 . peek ( ) ; stack2 . pop ( ) ; } } while ( ! stack1 . isEmpty ( ) ) { ans = ans * 10 + stack1 . peek ( ) ; stack1 . pop ( ) ; } while ( ! stack2 . isEmpty ( ) ) { ans = ans * 10 + stack2 . peek ( ) ; stack2 . pop ( ) ; } return ans ; } public static void main ( String [ ] args ) { int n1 = 64432 ; System . out . print ( minimumNo ( n1 ) + "NEW_LINE"); int n2 = 3137 ; System . out . print ( minimumNo ( n2 ) + "NEW_LINE"); } }