cppAcademy
FROM LEARNING TO COMPETING

Intermediate.
Put your skills to the test.

Bulgarian Informatics Group D · 2007–2026

270 independently explained problems: 54 Municipal, 54 Regional, 105 National Olympiad and 57 Autumn Tournament tasks. Start with the easier tasks and work upwards. Difficulty labels are learning estimates, not official ratings; Group D also includes challenging national-level problems. The six 2017 National tasks shared by Groups C and D use their existing permanent URLs. Years follow Arena’s competition-season labels. Source clarifications and special grader requirements are noted on affected tasks.
GROUP D2012 · EasySorting and checking

Mini Poker Consecutive Values

National Olympiad — Municipal round (2012 season)

Submission name: minipoker.cpp

Problem summary

Given five values, print them sorted if they form five consecutive integers. Otherwise print only their maximum.

Input

Five integers between 1 and 13.

Output

The five sorted consecutive integers, or just the maximum.

Constraints

Exactly five values, each in [1,13].

Worked example

Input and output for the worked example discussed below.

Input

1 5 2 4 3

Output

1 2 3 4 5

Solution analysis

  1. Sort the five values. Sorting exposes both the maximum, at the last position, and every possible gap between neighbouring values.
  2. The values are consecutive exactly when all four adjacent differences equal 1. This also rejects repeated values; checking only maximum−minimum=4 would not do that.
  3. The sample sorts to 1,2,3,4,5, so print all five. The alternative sample 1,1,4,1,4 has repeated values and gaps, so print its maximum 4 instead.

Why it works

A strictly consecutive sorted sequence has every adjacent difference 1. Conversely, if every difference is 1, each sorted value equals the first plus its index, which is precisely a set of five consecutive integers. The sorted final value is the maximum in every case.

Complexity: O(1) time and space for exactly five values; generalized to k values, sorting takes O(k log k) time and O(k) storage.

Watch out. Duplicates are not consecutive. Do not print the entire sorted list when the check fails. Five values are read even though there is no leading count.

Commented C++17 solution

An independently written complete program with standard headers.

#include<algorithm>
#include<array>
#include<cmath>
#include<iostream>
#include<limits>
#include<map>
#include<numeric>
#include<set>
#include<string>
#include<vector>
using namespace std;
int main(){
    array<int,5>values;
    for(int&value:values){
        cin>>value;
    }
    sort(values.begin(),values.end());
    bool consecutive=true;
    for(int index=1;index<5;index++){
        // Every sorted neighbour must differ by exactly one.
        if(values[index]!=values[index-1]+1){
            consecutive=false;
        }
    }
    if(!consecutive){
        cout<<values.back()<<'\n';
    }
    else{
        for(int index=0;index<5;index++){
            if(index>0){
                cout<<' ';
            }
            cout<<values[index];
        }
        cout<<'\n';
    }
}

Source: Arena Olimpiici ↗