cppAcademy
FROM LEARNING TO COMPETING

Real problems.
Your next challenge.

Explore the Group C Olympiad archive.
Choose a topic. Find a challenge. Build your solution.

213 tasks across the Municipal, Regional, and National rounds (2009–2026), plus 4 previously featured problems. 146 tasks have independently written English summaries, analyses, and C++17 solutions; the remaining entries link to Arena. Topic tags are from Arena or our reviewed explanations. Difficulty labels are learning estimates, not official ratings.
GROUP C2022 · MediumDigit Construction

Largest Seven-Segment Number

National Olympiad — Municipal round

Submission name: maxnum.cpp

Problem summary

Using N light segments of a conventional seven-segment display, construct the largest positive decimal integer.

Input

N.

Output

The largest number, or −1 when no digit can be formed.

Constraints

0 ≤ N ≤ 10⁷. Standard segment counts for digits 0…9 are 6,2,5,5,4,5,6,3,7,6.

Worked example

An independently prepared example for this explanation.

Input

7

Output

711

Solution analysis

  1. Maximize digit count first: digit 1 uses only two segments.
  2. For odd N≥3, put a 7 first and use 1 for every remaining pair. For even N use only 1.

Why it works

No digit costs fewer than two segments. At maximal length an even budget leaves no spare segment, while an odd budget leaves exactly one, best spent turning the leading 1 into 7.

Complexity: O(N) output time, O(1) auxiliary space.

Watch out. The answer may contain five million digits; stream it instead of parsing it numerically.

Commented C++17 solution

An independently written complete program with standard headers.

// Key idea: No digit costs fewer than two segments. At maximal length an even budget leaves no spare
// segment, while an odd budget leaves exactly one, best spent turning the leading 1 into 7.
#include<iostream>
#include<tuple>
using namespace std;
int main(){
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int n;
    cin>>n;
    if(n<2){
        cout<<-1<<'\n';
        return 0;
    }
    if(n%2){
        cout<<7;
        n-=3;
    }
    while(n){
        cout<<1;
        n-=2;
    }
    cout<<'\n';
}

Source: Arena Olimpiici ↗