Given an integer N, the task is to find the minimum integer that can be obtained from the given integer such that the adjacent digits of different parity can be swapped any no of times.
Two digits of different parity means that they will have different remainders when divided by two.
Examples:
Input: N = 64432
Output: 36442
Explanation:
Swap the 4th and 3rd digit; N = 64342
Swap the 3rd and 2nd digit; N = 63442
Swap the 2nd and 1st digit; N = 36442
Input :
3137
Output :
3137
Approach: The idea of the approach is to use two stacks to keep the digits of the number.
- In one stack, numbers which are divisible by two can be stored.
- In another stack, numbers which are not divisible by two can be stored.
- The elements are then pushed into both the stacks from reverse order of the number.
- Now, the element from the stack whose top contains the smaller element is popped and concatenated with the answer till one of the stacks is empty.
- The remaining elements of the stack are then concatenated which is not empty with the answer and finally the output is returned.
Below is the implementation of the above approach.
CPP
// C++ implementation of the above approach. #include <bits/stdc++.h> using namespace std; // Function to return the minimum number int minimumNo( int n) { int ans = 0; stack< int > stack1; stack< int > stack2; while (n != 0) { int r = n % 10; // Store the elements which are // divisible by two in stack1 if (r % 2 == 0) { stack1.push(r); } // Store the elements which are // not divisible by two in stack2. else { stack2.push(r); } n = n / 10; } while (!stack1.empty() && !stack2.empty()) { // Concatenate the answer with smaller value // of the topmost elements of both the // stacks and then pop that element if (stack1.top() < stack2.top()) { ans = ans * 10 + stack1.top(); stack1.pop(); } else { ans = ans * 10 + stack2.top(); stack2.pop(); } } // Concatenate the answer with remaining // values of stack1. while (!stack1.empty()) { ans = ans * 10 + stack1.top(); stack1.pop(); } // Concatenate the answer with remaining // values of stack2. while (!stack2.empty()) { ans = ans * 10 + stack2.top(); stack2.pop(); } return ans; } // Driver code int main() { int n1 = 64432; // Function calling cout << minimumNo(n1) << endl; int n2 = 3137; cout << minimumNo(n2) << endl; return 0; } |
Java
// Java implementation of the above approach. import java.util.*; class GFG { // Function to return the minimum number 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 ; // Store the elements which are // divisible by two in stack1 if (r % 2 == 0 ) { stack1.add(r); } // Store the elements which are // not divisible by two in stack2. else { stack2.add(r); } n = n / 10 ; } while (!stack1.isEmpty() && !stack2.isEmpty()) { // Concatenate the answer with smaller value // of the topmost elements of both the // stacks and then pop that element if (stack1.peek() < stack2.peek()) { ans = ans * 10 + stack1.peek(); stack1.pop(); } else { ans = ans * 10 + stack2.peek(); stack2.pop(); } } // Concatenate the answer with remaining // values of stack1. while (!stack1.isEmpty()) { ans = ans * 10 + stack1.peek(); stack1.pop(); } // Concatenate the answer with remaining // values of stack2. while (!stack2.isEmpty()) { ans = ans * 10 + stack2.peek(); stack2.pop(); } return ans; } // Driver code public static void main(String[] args) { int n1 = 64432 ; // Function calling System.out.print(minimumNo(n1) + "\n" ); int n2 = 3137 ; System.out.print(minimumNo(n2) + "\n" ); } } // This code is contributed by 29AjayKumar |
Python3
# Python3 implementation of the above approach. # Function to return the minimum number def minimumNo(n): ans = 0 stack1 = [] stack2 = [] while (n ! = 0 ): r = n % 10 # Store the elements which are # divisible by two in stack1 if (r % 2 = = 0 ): stack1.append(r) # Store the elements which are # not divisible by two in stack2. else : stack2.append(r) n = n / / 10 while ( len (stack1) > 0 and len (stack2) > 0 ): # Concatenate the answer with smaller value # of the topmost elements of both the # stacks and then pop that element if (stack1[ - 1 ] < stack2[ - 1 ]): ans = ans * 10 + stack1[ - 1 ] del stack1[ - 1 ] else : ans = ans * 10 + stack2[ - 1 ] del stack2[ - 1 ] # Concatenate the answer with remaining # values of stack1. while ( len (stack1) > 0 ): ans = ans * 10 + stack1[ - 1 ] del stack1[ - 1 ] # Concatenate the answer with remaining # values of stack2. while ( len (stack2) > 0 ): ans = ans * 10 + stack2[ - 1 ] del stack2[ - 1 ] return ans # Driver code n1 = 64432 # Function calling print (minimumNo(n1)) n2 = 3137 print (minimumNo(n2)) # This code is contributed by mohit kumar 29 |
C#
// C# implementation of the above approach. using System; using System.Collections.Generic; class GFG { // Function to return the minimum number 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; // Store the elements which are // divisible by two in stack1 if (r % 2 == 0) { stack1.Push(r); } // Store the elements which are // not divisible by two in stack2. else { stack2.Push(r); } n = n / 10; } while (stack1.Count != 0 && stack2.Count != 0) { // Concatenate the answer with smaller value // of the topmost elements of both the // stacks and then pop that element if (stack1.Peek() < stack2.Peek()) { ans = ans * 10 + stack1.Peek(); stack1.Pop(); } else { ans = ans * 10 + stack2.Peek(); stack2.Pop(); } } // Concatenate the answer with remaining // values of stack1. while (stack1.Count != 0) { ans = ans * 10 + stack1.Peek(); stack1.Pop(); } // Concatenate the answer with remaining // values of stack2. while (stack2.Count != 0) { ans = ans * 10 + stack2.Peek(); stack2.Pop(); } return ans; } // Driver code public static void Main(String[] args) { int n1 = 64432; // Function calling Console.Write(minimumNo(n1) + "\n" ); int n2 = 3137; Console.Write(minimumNo(n2) + "\n" ); } } // This code is contributed by PrinciRaj1992 |
Javascript
<script> // JavaScript implementation // of the above approach. // Function to return the minimum number function minimumNo(n) { var ans = 0; var stack1 = []; var stack2 = []; while (n !== 0) { var r = n % 10; // Store the elements which are // divisible by two in stack1 if (r % 2 === 0) { stack1.push(r); } // Store the elements which are // not divisible by two in stack2. else { stack2.push(r); } n = parseInt(n / 10); } while (stack1.length !== 0 && stack2.length !== 0) { // Concatenate the answer with smaller value // of the topmost elements of both the // stacks and then pop that element 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(); } } // Concatenate the answer with remaining // values of stack1. while (stack1.length !== 0) { ans = ans * 10 + stack1[stack1.length - 1]; stack1.pop(); } // Concatenate the answer with remaining // values of stack2. while (stack2.length !== 0) { ans = ans * 10 + stack2[stack2.length - 1]; stack2.pop(); } return ans; } // Driver code var n1 = 64432; // Function calling document.write(minimumNo(n1) + "<br>" ); var n2 = 3137; document.write(minimumNo(n2) + "<br>" ); </script> |
36442 3137
Time Complexity: O(logN)
Auxiliary Space: O(logN).
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!