Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6165801
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T22:13:47+00:00 2026-05-23T22:13:47+00:00

I made a program to solve this problem from the ACM. Matchsticks are ideal

  • 0

I made a program to solve this problem from the ACM.

Matchsticks are ideal tools to represent numbers. A common way to represent the ten decimal digits with matchsticks is the following:

This is identical to how numbers are displayed on an ordinary alarm clock. With a given number of matchsticks you can generate a wide range of numbers. We are wondering what the smallest and largest numbers are that can be created by using all your matchsticks.

Input

On the first line one positive number: the number of testcases, at most 100. After that per testcase:

One line with an integer n (2 ≤ n ≤ 100): the number of matchsticks you have.
Output

Per testcase:

One line with the smallest and largest numbers you can create, separated by a single space. Both numbers should be positive and contain no leading zeroes.
Sample Input

4
3
6
7
15
Sample Output

7 7
6 111
8 711
108 7111111

The problem is that it’s way too slow to solve it for 100 matchsticks. The search tree is too big to bruteforce it.

Here are the results for the first 10:

2: 1 1

3: 7 7

4: 4 11

5: 2 71

6: 6 111

7: 8 711

8: 10 1111

9: 18 7111

10: 22 11111

The pattern for the maximums is easy but I don’t see a shortcut for the minimums. Can someone suggest a better way to solve this problem? Here is the code I used:

    #include <iostream>
    #include <string>
    using namespace std;

    #define MAX 20 //should be 100

    //match[i] contains number of matches needed to form i
    int match[] = {6, 2, 5, 5, 4, 5, 6, 3, 7, 6};
    string mi[MAX+1], ma[MAX+1];
    char curr[MAX+1] = "";

    //compare numbers saved as strings
    int mycmp(string s1, string s2)
    {
        int n = (int)s1.length();
        int m = (int)s2.length();
        if (n != m)
            return n - m;
        else
            return s1.compare(s2);
    }

    //i is the current digit, used are the number of matchsticks so far
    void fill(int i, int used)
    {
        //check for smaller and bigger values
        if (mycmp(curr, mi[used]) < 0) mi[used] = curr;
        if (mycmp(curr, ma[used]) > 0) ma[used] = curr;

        //recurse further, don't start numbers with a zero
        for (int a = i ? '0' : '1'; a <= '9'; a++) {
            int next = used + match[a-'0'];
            if (next <= MAX) {
                curr[i] = a;
                curr[i+1] = '\0';
                fill(i + 1, next);
            }
        }
    }

    int main()
    {
        //initialise 
        for (int i = 0; i <= MAX; i++) {
            mi[i] = string(MAX, '9');
            ma[i] = "0";
        }

        //precalculate the values
        fill(0, 0);

        int n;
        cin >> n;

        //print those that were asked
        while (n--) {
            int num;
            cin >> num;
            cout << mi[num] << " " << ma[num] << endl;
        }

        return 0;
    }

EDIT : I ended up using the dynamic programming solution. I tried it with dp before but I was messing around with a two-dimensional state array. The solutions here are much better. Thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T22:13:48+00:00Added an answer on May 23, 2026 at 10:13 pm

    In order to find the result :

    • first find the minimal number of digits for smallest number
    • then proceed from most significant digit to the least significant one.

    Every digit should be chosen so that there exists a solution for remaining digits.
    Each digit requires between 2 and 7 matches. So you must choose the smallest Nth “top” digit that leaves the number of remaining matches between 2*(N-1) and 7*(N-1).

    Do not forget that 0 has to be excluded from the search for the most significant digit of the result.

    As a sidenote, one reason which makes this algorithm work is the fact that there is at least one corresponding digit for every value (of matches) between 2 and 7.

    EDIT : example for 10 matches
    10 matches –> 2 digits
    Acceptable number of matches for top digit = between 3 and 7.
    Smallest digit which requires between 3 and 7 matches -> 2 (which takes 5 matches), 0 being excluded.
    Chosen first digit = 2

    5 remaining matches –>
    Acceptable number of matches for second (and in this case last) digit = exactly 5
    Smallest digit which requires 5 matches -> 2
    Chosen second digit = 2

    Result = 22.

    EDIT code for this problem

    #include <iostream>
    #include <vector>
    
    std::vector<int> nbMatchesForDigit;
    
    long long minNumberForMatches(unsigned int nbMatches)
    {
        int nbMaxMatchesForOneDigit = 7;
        int nbMinMatchesForOneDigit = 2;
        int remainingMatches = nbMatches;
        int nbDigits = 1 + nbMatches / nbMaxMatchesForOneDigit; 
        long long result = 0;
        for (int idDigit = 0 ; idDigit < nbDigits ; ++idDigit )
        {
            int minMatchesToUse = std::max(nbMinMatchesForOneDigit, remainingMatches - nbMaxMatchesForOneDigit * (nbDigits - 1 - idDigit));
            int maxMatchesToUse = std::min(nbMaxMatchesForOneDigit, remainingMatches - nbMinMatchesForOneDigit * (nbDigits - 1 - idDigit));
            for (int digit = idDigit > 0 ? 0 : 1 ; digit <= 9 ; ++digit )
            {
                if( nbMatchesForDigit[digit] >= minMatchesToUse && 
                    nbMatchesForDigit[digit] <= maxMatchesToUse )
                {
                    result = result * 10 + digit;
                    remainingMatches -= nbMatchesForDigit[digit];
                    break;
                }
            }
        }
        return result;
    }
    
    int main()
    {
        nbMatchesForDigit.push_back(6);
        nbMatchesForDigit.push_back(2);
        nbMatchesForDigit.push_back(5);
        nbMatchesForDigit.push_back(5);
        nbMatchesForDigit.push_back(4);
        nbMatchesForDigit.push_back(5);
        nbMatchesForDigit.push_back(6);
        nbMatchesForDigit.push_back(3);
        nbMatchesForDigit.push_back(7);
        nbMatchesForDigit.push_back(6);
    
        for( int i = 2 ; i <= 100 ; ++i )
        {
            std::cout << i << " " << minNumberForMatches(i) << std::endl;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I manage to solve this problem getting hint from post stackoverflow post about GWT
I have a program i frequently use that is made with .NET. This program
I made this small Java program using eclipse IDE. I have set the workspace
I have a problem here which i am trying to solve. The program is
I made a program. I also made my own file type, which the program
I made small program to divide large pictures and take part of them. When
I made a program that opens an application, sleeps the thread for 500ms then
I have made a program in c and wanted to see, how much memory
I'm a beginner in programming. I've just made a program called Guessing Game. And
I have made a registration program. Making use of mysql database. Can I still

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.