How to Attempt?
Wedding Game
In a wedding that you are attending, there are some chairs that have digits inscribed at their backs. The chairs are lined in a row such that they form a string of the digits. Find the minimum number of sets M that can be formed from these digits such that:
1. The number of digits in each set is one or more than one. 2. Each set is formed using consecutive digits and no digit can be used more
than once.
3. In each set, the number formed using the digits is less than or equal to Y.
Input Specification:
input1: S, string of digits
input2: Y. No number should be greater than Y
input3: Size of the String S
Output Specification:
Your function should return M, the minimum number of sets
#include <iostream>
#include <string>
using namespace std;
int minimumSets(string input, int y) {
int count = 0;
int number = 0;
int l = input.length();
int x = 0;
for (int i = 0; i < l; ++i) {
number = number * 10 + input[i] - 48;
if (number <= y) {
x = 1;
continue;
}
if (x) {
++count;
}
number = input[i] - '0';
x = 0;
if (number <= y) {
x = 1;
} else {
number = 0;
}
}
if (x) {
++count;
}
return count;
}
int main() {
string input = "1234";
int y = 20;
cout << "Minimum sets are = " << minimumSets(input, y) << endl;
return 0;
}
Comments
Leave a comment