Minimum integer that can be obtained by swapping adjacent digits of different parity | 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
function minimumNo ( n ) { var ans = 0 ; var stack1 = [ ] ; var stack2 = [ ] ; while ( n !== 0 ) { var r = n % 10 ; if ( r % 2 === 0 ) { stack1 . push ( r ) ; } else { stack2 . push ( r ) ; } n = parseInt ( n / 10 ) ; } while ( stack1 . length !== 0 && stack2 . length !== 0 ) { if ( stack1 [ stack1 . length - 1 ] < stack2 [ stack2 . length - 1 ] ) { ans = ans * 10 + stack1 [ stack1 . length - 1 ] ; stack1 . pop ( ) ; } else { ans = ans * 10 + stack2 [ stack2 . length - 1 ] ; stack2 . pop ( ) ; } } while ( stack1 . length !== 0 ) { ans = ans * 10 + stack1 [ stack1 . length - 1 ] ; stack1 . pop ( ) ; } while ( stack2 . length !== 0 ) { ans = ans * 10 + stack2 [ stack2 . length - 1 ] ; stack2 . pop ( ) ; } return ans ; } var n1 = 64432 ; document . write ( minimumNo ( n1 ) + " " ) ; var n2 = 3137 ; document . write ( minimumNo ( n2 ) + " " ) ;