Smallest number possible by swapping adjacent even odd pairs | C # program to find the smallest number possible by swapping adjacent digits of different parity ; Function to return the smallest number possible ; Arrays to store odd and even digits in the order their appearance in the given String ; Insert the odd and even digits ; Pointer to odd digit ; Pointer to even digit ; In case number of even and odd digits are not equal If odd digits are remaining ; If even digits are remaining ; Removal of leading 0 's ; Driver code
using System ; using System . Collections . Generic ; class GFG { static String findAns ( String s ) { int digit ; List < int > odd = new List < int > ( ) ; List < int > even = new List < int > ( ) ; foreach ( char c in s . ToCharArray ( ) ) { digit = c - '0' ; if ( digit % 2 == 1 ) odd . Add ( digit ) ; else even . Add ( digit ) ; } int i = 0 ; int j = 0 ; String ans = " " ; while ( i < odd . Count && j < even . Count ) { if ( odd [ i ] < even [ j ] ) ans += ( char ) ( odd [ i ++ ] + '0' ) ; else ans += ( char ) ( even [ j ++ ] + '0' ) ; } while ( i < odd . Count ) ans += ( char ) ( odd [ i ++ ] + '0' ) ; while ( j < even . Count ) ans += ( char ) ( even [ j ++ ] + '0' ) ; while ( ans [ 0 ] == '0' ) { ans = ans . Substring ( 1 ) ; } return ans ; } public static void Main ( String [ ] args ) { String s = "894687536" ; Console . Write ( findAns ( s ) ) ; } }