Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 1f5c065

Browse files
committed
Prob 185: add lowest possible numbers
1 parent cc540dc commit 1f5c065

File tree

2 files changed

+78
-1
lines changed

2 files changed

+78
-1
lines changed

‎README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
| Current Status| Stats |
88
| :------------: | :----------: |
9-
| Total Problems | 184 |
9+
| Total Problems | 185 |
1010

1111
</center>
1212

@@ -166,6 +166,7 @@ Include contains single header implementation of data structures and some algori
166166
| Implement pow(x,y) using divide and conquer approach. Try implementing it in O(logn)| [pow.cpp](math_problems/pow.cpp)|
167167
| Calculate factorial of large number, say 100 (it will have 158 digits) |[factorial_of_large_num.cpp](math_problems/factorial_of_large_num.cpp)|
168168
| Generate all possible words from a number entered on a traditional mobile keypad | [phone_digits.cpp](math_problems/phone_digits.cpp)|
169+
| Given a string representation of a number, remove n characters from the string such that number representation is lowest possible.| [lowest_possible_number.cpp](math_problems/lowest_possible_number.cpp)|
169170

170171
### Stack Problems
171172
| Problem | Solution |
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
/*
2+
* Problem:
3+
* Given a string representation of a number, remove n characters
4+
* from the string such that number representation is lowest possible.
5+
*
6+
* Example:
7+
* Input:
8+
* str = "4325043", n = 3
9+
* Output:
10+
* "2043"
11+
*
12+
* Input:
13+
* str = "765028321", n = 5
14+
* Output:
15+
* "0221"
16+
*
17+
* Input:
18+
* str = "12345", n = 6
19+
* Output:
20+
* 0
21+
*
22+
* Input:
23+
* str = "12345", n = 0
24+
* Output:
25+
* 12345
26+
*/
27+
28+
#include <iostream>
29+
30+
31+
void build_lowest_number(const std::string& num, std::string& result,
32+
int n)
33+
{
34+
// If 0 characters are left to remove, append everything to result.
35+
if (n == 0) {
36+
result.append(num);
37+
return;
38+
}
39+
40+
int len = num.length();
41+
42+
// If there are more characters to remove than string length.
43+
// remove nothing and return, last example above.
44+
if (n >= len) {
45+
return;
46+
}
47+
48+
// Now, we will find the minimum digit in n+1 characters.
49+
// Since we need to remove, smallest of n+1 digits should be
50+
// part of final string.
51+
int min_index = 0;
52+
for (int i = 1; i <= n; ++i) {
53+
if (num[i] < num[min_index]) {
54+
min_index = i;
55+
}
56+
}
57+
58+
result.push_back(num[min_index]);
59+
std::string remaining_num = num.substr(min_index + 1,
60+
len - min_index);
61+
62+
build_lowest_number(remaining_num, result, n-min_index);
63+
}
64+
65+
66+
int main()
67+
{
68+
std::string num_string{"4325043"};
69+
std::string result;
70+
std::cout << "Number: " << num_string << std::endl;
71+
std::cout << "After removing 5 characters, smallest possible number:"
72+
<< std::endl;
73+
build_lowest_number(num_string, result, 3);
74+
std::cout << result << std::endl;
75+
return 0;
76+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /