You are given a string num
representing a non-negative integer. Rearrange the digits to form the smallest possible number that is divisible by three. If the input cannot be rearranged to a number divisible by three, return an empty string.
0-9
)?#include <iostream>
#include <vector>
#include <algorithm>
std::string smallestDivisibleByThree(std::string num) {
if (num.empty()) return "";
// Calculate the sum of digits
int totalSum = 0;
for (char ch : num) {
if (!isdigit(ch)) return "";
totalSum += (ch - '0');
}
// Check if current digits already divisible by 3
if (totalSum % 3 == 0) {
// sort digits to get smallest number
std::sort(num.begin(), num.end());
return num;
}
std::vector<int> digit_count(10, 0);
for (char ch : num) {
digit_count[ch - '0']++;
}
auto remove_digit = [&](int rem) {
// Try to remove one digit with remainder 'rem'
for (int i = rem; i < 10; i += 3) {
if (digit_count[i] > 0) {
digit_count[i]--;
return true;
}
}
return false;
};
bool adjusted = false;
int remainder = totalSum % 3;
// If totalSum % 3 is 1, try to remove one digit with remainder 1.
// If not, remove two digits each with remainder 2.
if (remainder == 1) {
adjusted = remove_digit(1) || (remove_digit(2) && remove_digit(2));
}
// If totalSum % 3 is 2, try to remove one digit with remainder 2.
// If not, remove two digits each with remainder 1.
else if (remainder == 2) {
adjusted = remove_digit(2) || (remove_digit(1) && remove_digit(1));
}
if (!adjusted) return "";
std::string result;
for (int i = 0; i < 10; ++i) {
result += std::string(digit_count[i], '0' + i);
}
// Remove leading zeros unless it's the only digit
int pos = 0;
while (pos < (int)result.size() - 1 && result[pos] == '0') ++pos;
result = result.substr(pos);
return result.empty() ? "" : result;
}
int main() {
std::string num = "123456";
std::string result = smallestDivisibleByThree(num);
std::cout << "Result: " << result << std::endl; // Should print the smallest number divisible by three
return 0;
}
num
.Got blindsided by a question you didn’t expect?
Spend too much time studying?
Or simply don’t have the time to go over all 3000 questions?