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
#include <bits/stdc++.h> NEW_LINE using namespace std ; int minimumNo ( int n ) { int ans = 0 ; stack < int > stack1 ; stack < int > stack2 ; while ( n != 0 ) { int r = n % 10 ; if ( r % 2 == 0 ) { stack1 . push ( r ) ; } else { stack2 . push ( r ) ; } n = n / 10 ; } while ( ! stack1 . empty ( ) && ! stack2 . empty ( ) ) { if ( stack1 . top ( ) < stack2 . top ( ) ) { ans = ans * 10 + stack1 . top ( ) ; stack1 . pop ( ) ; } else { ans = ans * 10 + stack2 . top ( ) ; stack2 . pop ( ) ; } } while ( ! stack1 . empty ( ) ) { ans = ans * 10 + stack1 . top ( ) ; stack1 . pop ( ) ; } while ( ! stack2 . empty ( ) ) { ans = ans * 10 + stack2 . top ( ) ; stack2 . pop ( ) ; } return ans ; } int main ( ) { int n1 = 64432 ; cout << minimumNo ( n1 ) << endl ; int n2 = 3137 ; cout << minimumNo ( n2 ) << endl ; return 0 ; }