Minimum integer that can be obtained by swapping adjacent digits of different parity | C # 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
using System ; using System . Collections . Generic ; class GFG { static int minimumNo ( int n ) { int ans = 0 ; Stack < int > stack1 = new Stack < int > ( ) ; Stack < int > stack2 = new Stack < int > ( ) ; while ( n != 0 ) { int r = n % 10 ; if ( r % 2 == 0 ) { stack1 . Push ( r ) ; } else { stack2 . Push ( r ) ; } n = n / 10 ; } while ( stack1 . Count != 0 && stack2 . Count != 0 ) { if ( stack1 . Peek ( ) < stack2 . Peek ( ) ) { ans = ans * 10 + stack1 . Peek ( ) ; stack1 . Pop ( ) ; } else { ans = ans * 10 + stack2 . Peek ( ) ; stack2 . Pop ( ) ; } } while ( stack1 . Count != 0 ) { ans = ans * 10 + stack1 . Peek ( ) ; stack1 . Pop ( ) ; } while ( stack2 . Count != 0 ) { ans = ans * 10 + stack2 . Peek ( ) ; stack2 . Pop ( ) ; } return ans ; } public static void Main ( String [ ] args ) { int n1 = 64432 ; Console . Write ( minimumNo ( n1 ) + " STRNEWLINE " ) ; int n2 = 3137 ; Console . Write ( minimumNo ( n2 ) + " STRNEWLINE " ) ; } }