Number Checker | Solution | C++

Raunit Verma - in Coding
Views: 0

Given an integer array as input, find out the integers which contain the digits 1, 2, and 3 in their formation

Number Checker | Find Numbers Containing Digits 1, 2, and 3


Given an integer array as input, find out the integers which contain the digits 1, 2, and 3 in their formation. The order of 1, 2, and 3 need not be sequential.

• In case multiple numbers qualifies the condition, The outcome should be ordered based on numeric value (lesser number first followed by greater number and so on..) and separated by comma.
• In case no number qualifies the conditions, it should return -1.

Example:
Input:
1456
345671
43218
123

Output:
123,43218

Explanation:
In the above example, the numbers 43218 and 123 contain the digits 1, 2, and 3. Sorting them in ascending order gives the final output: 123,43218.

Solution
string findQualifiedNumbers(vector<int> numberArray)
{
    string ans = "";
    vector<int> res;

    for (auto i : numberArray)
    {
        string t = to_string(i);
        vector<int> v(4, 0);
        for (auto c : t)
            if (c - '0' <= 3 && c - '0' >= 1)
                v[c - '0']++;
        if (v[1] != 0 && v[2] != 0 && v[3] != 0)
            res.push_back(i);
    }
    sort(res.begin(), res.end());
    for (int i = 0; i < res.size(); i++)
    {
        ans += to_string(res[i]);
        if (i != res.size() - 1)
            ans += ",";
    }
    if (res.size() == 0)
        return "-1";
    return ans;
}
Tagsnumber checkerc++OA 2025codinggoldman sachs
Raunit Verma-picture
Raunit Verma

Technical Writer at CodingKaro

Share this on
CodingKaro Poster
CodingKaro
4.7

3K+ Downloads

One of its unique features is an automated coding contest reminder, which sets alarms for upcoming coding contests on popular platforms like CodeChef, CodeForces, and LeetCode, without the need for user input. This feature ensures that users never miss a coding contest and can stay updated with the latest coding challenges.

Download CodingKaro
Other Blogs in Coding