Posted on by Kalkicode
Code Probability

# Find smallest permutation of given number

A permutation of a number is simply rearranging its digits to create a new number. The smallest permutation of a given number is the rearrangement that results in the smallest possible number. Without reducing its digits of decimal number.

For example, let's say we have the number 312. Starting from the leftmost digit, we compare 3 to 1 and 2. Since 1 is the smallest digit larger than 3, we swap them to get 132. Finally, we sort the digits to the right of the swapped digits in ascending order to get 123, which is the smallest permutation of the original number.

If number is 745809023 then result is 200345789.

## Code Solution

``````// Java Program for
// Find smallest permutation of given number
public class Permutation
{
public void smallestPermutation(String num)
{
int n = num.length();
if (n == 0)
{
return;
}
// Assume num is valid number
int[] digit = new int[10];
String zero = "";
String result = "";
for (int index = 0; index < 10; ++index)
{
digit[index] = 0;
}
// Count the frequency of digit in given number
for (int index = 0; index < n; ++index)
{
if (num.charAt(index) == '0')
{
// Collect zero
zero += "0";
}
else
{
// Count frequency of digit
digit[num.charAt(index) - '0'] += 1;
}
}
for (int index = 1; index < 10; ++index)
{
if (digit[index] != 0)
{
while (digit[index] > 0)
{
result += "" + index;
digit[index]--;
}
// When in case zero exist
result += zero;
zero = "";
}
}
// In case only zero
result += zero;
System.out.println("Given Number : " + num);
System.out.println("Result : " + result);
}
public static void main(String[] args)
{
// Test
// num    = "4851233"
// output = "1233458"
// num    = "745809023"
// output = "200345789"
}
}``````

#### Output

``````Given Number : 4851233
Result : 1233458
Given Number : 745809023
Result : 200345789``````
``````// Include header file
#include <iostream>

#include <string>

using namespace std;
// C++ Program for
// Find smallest permutation of given number
class Permutation
{
public: void smallestPermutation(string num)
{
int n = num.length();
if (n == 0)
{
return;
}
// Assume num is valid number
int digit[10];
string zero = "";
string result = "";
for (int index = 0; index < 10; ++index)
{
digit[index] = 0;
}
// Count the frequency of digit in given number
for (int index = 0; index < n; ++index)
{
if (num[index] == '0')
{
// Collect zero
zero += "0";
}
else
{
// Count frequency of digit
digit[num[index] - '0'] += 1;
}
}
for (int index = 1; index < 10; ++index)
{
if (digit[index] != 0)
{
while (digit[index] > 0)
{
result += ""
+  to_string(index);
digit[index]--;
}
// When in case zero exist
result += zero;
zero = "";
}
}
// In case only zero
result += zero;
cout << "Given Number : " << num << endl;
cout << "Result : " << result << endl;
}
};
int main()
{
// Test
// num    = "4851233"
// output = "1233458"
// num    = "745809023"
// output = "200345789"
return 0;
}``````

#### Output

``````Given Number : 4851233
Result : 1233458
Given Number : 745809023
Result : 200345789``````
``````package main
import "strconv"
import "fmt"
// Go Program for
// Find smallest permutation of given number
type Permutation struct {}
func getPermutation() * Permutation {
var me *Permutation = &Permutation {}
return me
}
func(this Permutation) smallestPermutation(num string) {
var n int = len(num)
if n == 0 {
return
}
// Assume num is valid number
var digit = make([] int, 10)
var zero string = ""
var result string = ""
for index := 0 ; index < 10 ; index++ {
digit[index] = 0
}
// Count the frequency of digit in given number
for index := 0 ; index < n ; index++ {
if num[index] == '0' {
// Collect zero
zero += "0"
} else {
// Count frequency of digit
digit[num[index] - '0'] += 1
}
}
for index := 1 ; index < 10 ; index++ {
if digit[index] != 0 {
for (digit[index] > 0) {
result += strconv.Itoa(index)
digit[index]--
}
// When in case zero exist
result += zero
zero = ""
}
}
// In case only zero
result += zero
fmt.Println("Given Number : ", num)
fmt.Println("Result : ", result)
}
func main() {
var task * Permutation = getPermutation()
// Test
// num    = "4851233"
// output = "1233458"
// num    = "745809023"
// output = "200345789"
}``````

#### Output

``````Given Number : 4851233
Result : 1233458
Given Number : 745809023
Result : 200345789``````
``````// Include namespace system
using System;
// Csharp Program for
// Find smallest permutation of given number
public class Permutation
{
public void smallestPermutation(String num)
{
int n = num.Length;
if (n == 0)
{
return;
}
// Assume num is valid number
int[] digit = new int[10];
String zero = "";
String result = "";
for (int index = 0; index < 10; ++index)
{
digit[index] = 0;
}
// Count the frequency of digit in given number
for (int index = 0; index < n; ++index)
{
if (num[index] == '0')
{
// Collect zero
zero += "0";
}
else
{
// Count frequency of digit
digit[num[index] - '0'] += 1;
}
}
for (int index = 1; index < 10; ++index)
{
if (digit[index] != 0)
{
while (digit[index] > 0)
{
result += "" + index;
digit[index]--;
}
// When in case zero exist
result += zero;
zero = "";
}
}
// In case only zero
result += zero;
Console.WriteLine("Given Number : " + num);
Console.WriteLine("Result : " + result);
}
public static void Main(String[] args)
{
// Test
// num    = "4851233"
// output = "1233458"
// num    = "745809023"
// output = "200345789"
}
}``````

#### Output

``````Given Number : 4851233
Result : 1233458
Given Number : 745809023
Result : 200345789``````
``````<?php
// Php Program for
// Find smallest permutation of given number
class Permutation
{
public	function smallestPermutation(\$num)
{
\$n = strlen(\$num);
if (\$n == 0)
{
return;
}
// Assume num is valid number
\$digit = array_fill(0, 10, 0);
\$zero = "";
\$result = "";
// Count the frequency of digit in given number
for (\$index = 0; \$index < \$n; ++\$index)
{
if (\$num[\$index] == '0')
{
// Collect zero
\$zero .= "0";
}
else
{
// Count frequency of digit
\$digit[ord(\$num[\$index]) - ord('0')] += 1;
}
}
for (\$index = 1; \$index < 10; ++\$index)
{
if (\$digit[\$index] != 0)
{
while (\$digit[\$index] > 0)
{
\$result .= "".strval(\$index);
\$digit[\$index]--;
}
// When in case zero exist
\$result .= \$zero;
\$zero = "";
}
}
// In case only zero
\$result .= \$zero;
echo("Given Number : ".\$num.
"\n");
echo("Result : ".\$result.
"\n");
}
}

function main()
{
// Test
// num    = "4851233"
// output = "1233458"
// num    = "745809023"
// output = "200345789"
}
main();``````

#### Output

``````Given Number : 4851233
Result : 1233458
Given Number : 745809023
Result : 200345789``````
``````// Node JS Program for
// Find smallest permutation of given number
class Permutation
{
smallestPermutation(num)
{
var n = num.length;
if (n == 0)
{
return;
}
// Assume num is valid number
var digit = Array(10).fill(0);
var zero = "";
var result = "";
// Count the frequency of digit in given number
for (var index = 0; index < n; ++index)
{
if (num.charAt(index) == '0')
{
// Collect zero
zero += "0";
}
else
{
// Count frequency of digit
digit[num.charAt(index).charCodeAt(0) - '0'.charCodeAt(0)] += 1;
}
}
for (var index = 1; index < 10; ++index)
{
if (digit[index] != 0)
{
while (digit[index] > 0)
{
result += "" + index;
digit[index]--;
}
// When in case zero exist
result += zero;
zero = "";
}
}
// In case only zero
result += zero;
console.log("Given Number : " + num);
console.log("Result : " + result);
}
}

function main()
{
// Test
// num    = "4851233"
// output = "1233458"
// num    = "745809023"
// output = "200345789"
}
main();``````

#### Output

``````Given Number : 4851233
Result : 1233458
Given Number : 745809023
Result : 200345789``````
``````#  Python 3 Program for
#  Find smallest permutation of given number
class Permutation :
def smallestPermutation(self, num) :
n = len(num)
if (n == 0) :
return

#  Assume num is valid number
digit = [0] * (10)
zero = ""
result = ""
index = 0
#  Count the frequency of digit in given number
while (index < n) :
if (num[index] == '0') :
#  Collect zero
zero += "0"
else :
#  Count frequency of digit
digit[ord(num[index]) - ord('0')] += 1

index += 1

index = 1
while (index < 10) :
if (digit[index] != 0) :
while (digit[index] > 0) :
result += str(index)
digit[index] -= 1

#  When in case zero exist
result += zero
zero = ""

index += 1

#  In case only zero
result += zero
print("Given Number : ", num)
print("Result : ", result)

def main() :
#  Test
#  num    = "4851233"
#  output = "1233458"
#  num    = "745809023"
#  output = "200345789"

if __name__ == "__main__": main()``````

#### Output

``````Given Number :  4851233
Result :  1233458
Given Number :  745809023
Result :  200345789``````
``````#  Ruby Program for
#  Find smallest permutation of given number
class Permutation
def smallestPermutation(num)
n = num.length
if (n == 0)
return
end

#  Assume num is valid number
digit = Array.new(10) {0}
zero = ""
result = ""
index = 0
#  Count the frequency of digit in given number
while (index < n)
if (num[index] == '0')
#  Collect zero
zero += "0"
else

#  Count frequency of digit
digit[num[index].ord - '0'.ord] += 1
end

index += 1
end

index = 1
while (index < 10)
if (digit[index] != 0)
while (digit[index] > 0)
result += index.to_s
digit[index] -= 1
end

#  When in case zero exist
result += zero
zero = ""
end

index += 1
end

#  In case only zero
result += zero
print("Given Number : ", num, "\n")
print("Result : ", result, "\n")
end

end

def main()
#  Test
#  num    = "4851233"
#  output = "1233458"
#  num    = "745809023"
#  output = "200345789"
end

main()``````

#### Output

``````Given Number : 4851233
Result : 1233458
Given Number : 745809023
Result : 200345789
``````
``````import scala.collection.mutable._;
// Scala Program for
// Find smallest permutation of given number
class Permutation()
{
def smallestPermutation(num: String): Unit = {
var n: Int = num.length();
if (n == 0)
{
return;
}
// Assume num is valid number
var digit: Array[Int] = Array.fill[Int](10)(0);
var zero: String = "";
var result: String = "";
var index: Int = 0;
// Count the frequency of digit in given number
while (index < n)
{
if (num.charAt(index) == '0')
{
// Collect zero
zero += "0";
}
else
{
// Count frequency of digit
digit(num.charAt(index).toInt - '0'.toInt) += 1;
}
index += 1;
}
index = 1;
while (index < 10)
{
if (digit(index) != 0)
{
while (digit(index) > 0)
{
result += "" + index.toString();
digit(index) -= 1;
}
// When in case zero exist
result += zero;
zero = "";
}
index += 1;
}
// In case only zero
result += zero;
println("Given Number : " + num);
println("Result : " + result);
}
}
object Main
{
def main(args: Array[String]): Unit = {
var task: Permutation = new Permutation();
// Test
// num    = "4851233"
// output = "1233458"
// num    = "745809023"
// output = "200345789"
}
}``````

#### Output

``````Given Number : 4851233
Result : 1233458
Given Number : 745809023
Result : 200345789``````
``````import Foundation;
// Swift 4 Program for
// Find smallest permutation of given number
class Permutation
{
func smallestPermutation(_ value: String)
{	let num = Array(value);
let n: Int = num.count;
if (n == 0)
{
return;
}
// Assume num is valid number
var digit: [Int] = Array(repeating: 0, count: 10);
var zero: String = "";
var result: String = "";
var index: Int = 0;
// Count the frequency of digit in given number
while (index < n)
{
if (num[index] == "0")
{
// Collect zero
zero += "0";
}
else
{
// Count frequency of digit
digit[Int(UnicodeScalar(String(num[index]))!.value) -
Int(UnicodeScalar(String("0"))!.value)] += 1;
}
index += 1;
}
index = 1;
while (index < 10)
{
if (digit[index]  != 0)
{
while (digit[index] > 0)
{
result +=  String(index);
digit[index] -= 1;
}
// When in case zero exist
result += zero;
zero = "";
}
index += 1;
}
// In case only zero
result += zero;
print("Given Number : ", value);
print("Result : ", result);
}
}
func main()
{
// Test
// num    = "4851233"
// output = "1233458"
// num    = "745809023"
// output = "200345789"
}
main();``````

#### Output

``````Given Number :  4851233
Result :  1233458
Given Number :  745809023
Result :  200345789``````
``````// Kotlin Program for
// Find smallest permutation of given number
class Permutation
{
fun smallestPermutation(num: String): Unit
{
val n: Int = num.length;
if (n == 0)
{
return;
}
// Assume num is valid number
val digit: Array < Int > = Array(10)
{
0
};
var zero: String = "";
var result: String = "";
var index: Int = 0;
// Count the frequency of digit in given number
while (index < n)
{
if (num.get(index) == '0')
{
// Collect zero
zero += "0";
}
else
{
// Count frequency of digit
digit[num.get(index).toInt() - '0'.toInt()] += 1;
}
index += 1;
}
index = 1;
while (index < 10)
{
if (digit[index] != 0)
{
while (digit[index] > 0)
{
result += index.toString();
digit[index] -= 1;
}
// When in case zero exist
result += zero;
zero = "";
}
index += 1;
}
// In case only zero
result += zero;
println("Given Number : " + num);
println("Result : " + result);
}
}
fun main(args: Array < String > ): Unit
{
// Test
// num    = "4851233"
// output = "1233458"
// num    = "745809023"
// output = "200345789"
}``````

#### Output

``````Given Number : 4851233
Result : 1233458
Given Number : 745809023
Result : 200345789``````

## Comment

Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

Categories
Relative Post