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
#include <bits/stdc++.h>
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;
}
|
Java
import java.util.*;
class GFG
{
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 ;
if (r % 2 == 0 )
{
stack1.add(r);
}
else
{
stack2.add(r);
}
n = n / 10 ;
}
while (!stack1.isEmpty() && !stack2.isEmpty())
{
if (stack1.peek() < stack2.peek())
{
ans = ans * 10 + stack1.peek();
stack1.pop();
}
else
{
ans = ans * 10 + stack2.peek();
stack2.pop();
}
}
while (!stack1.isEmpty())
{
ans = ans * 10 + stack1.peek();
stack1.pop();
}
while (!stack2.isEmpty())
{
ans = ans * 10 + stack2.peek();
stack2.pop();
}
return ans;
}
public static void main(String[] args)
{
int n1 = 64432 ;
System.out.print(minimumNo(n1) + "\n" );
int n2 = 3137 ;
System.out.print(minimumNo(n2) + "\n" );
}
}
|
Python3
def minimumNo(n):
ans = 0
stack1 = []
stack2 = []
while (n ! = 0 ):
r = n % 10
if (r % 2 = = 0 ):
stack1.append(r)
else :
stack2.append(r)
n = n / / 10
while ( len (stack1) > 0 and len (stack2) > 0 ):
if (stack1[ - 1 ] < stack2[ - 1 ]):
ans = ans * 10 + stack1[ - 1 ]
del stack1[ - 1 ]
else :
ans = ans * 10 + stack2[ - 1 ]
del stack2[ - 1 ]
while ( len (stack1) > 0 ):
ans = ans * 10 + stack1[ - 1 ]
del stack1[ - 1 ]
while ( len (stack2) > 0 ):
ans = ans * 10 + stack2[ - 1 ]
del stack2[ - 1 ]
return ans
n1 = 64432
print (minimumNo(n1))
n2 = 3137
print (minimumNo(n2))
|
C#
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) + "\n" );
int n2 = 3137;
Console.Write(minimumNo(n2) + "\n" );
}
}
|
Javascript
<script>
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) + "<br>" );
var n2 = 3137;
document.write(minimumNo(n2) + "<br>" );
</script>
|
Time Complexity: O(logN)
Auxiliary Space: O(logN).
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!
Last Updated :
26 Nov, 2021
Like Article
Save Article