-
Notifications
You must be signed in to change notification settings - Fork 2
/
smallest_num_rearranging_digit_num.cpp
123 lines (98 loc) · 2.44 KB
/
smallest_num_rearranging_digit_num.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
/****************************************************************************
File name: smallest_num_rearranging_digit_num.cpp
Author: babajr
*****************************************************************************/
/*
Find the Smallest number (Not leading Zeros) which can be obtained by
rearranging the digits of a given number.
Input: n = 846903
Output: 304689
Input: n = 55010
Output: 10055
Input: n = -40505
Output: -55400
*/
#include<bits/stdc++.h>
using namespace std;
/*
API to get the smallest number after rearranging the digits of the number.
Algo:
--> Count the frequency of the digits using frequency[].
--> Get the left most digit i.e. smallest digit other than 0.
--> Arrange remaining digits in ascending order.
*/
int smallest(int num)
{
// frequency array to count the frequency of digits.
int frequency[10] = {0};
int res = 0;
while(num > 0)
{
int digit = num % 10;
frequency[digit]++;
num = num / 10;
}
// get the smallest digit as the highest digit of a number
// starting from 1 as smallest number starting other than 0 is asked.
for(int i = 1; i <= 9; i++)
{
if(frequency[i] > 0)
{
res = i;
frequency[i]--;
break;
}
}
// arrange the other digits in ascending order in res variable.
for(int j = 0; j <= 9; j++)
{
while(frequency[j]-- > 0)
{
res = res * 10 + j;
}
}
return res;
}
/*
API to get largest num after rearranging the digits of the number.
*/
int largest(int num)
{
// frequency array to count the frequency of digits.
int frequency[10] = {0};
int res = 0;
while(num > 0)
{
int digit = num % 10;
frequency[digit]++;
num = num / 10;
}
// get the largest digit as the highest digit of a number
for(int i = 9; i >= 0; i--)
{
if(frequency[i] > 0)
{
res = i;
frequency[i]--;
break;
}
}
// arrange the other digits in descending order in res variable.
for(int j = 9; j >= 0; j--)
{
while(frequency[j]-- > 0)
{
res = res * 10 + j;
}
}
return res;
}
int main(void)
{
int num = 846903;
printf("Smallest Number after rearrangement of digits : %d\n",
smallest(num));
printf("Largest Number after rearrangement of digits : %d\n",
largest(num));
return 0;
}