CP: Smallest, largest and how often

The input holds whole numbers, but you are not told how many. Read them all, then report the smallest, the largest, and how many times the largest one appears.

What to build

Read all the numbers with while (cin >> x). Keep the smallest, the largest and a count of how often the largest has been seen, and print them on one line.

Requirements

  • Read until the input ends
  • Print SMALLEST LARGEST COUNT
  • Use long long for the numbers

Input

At least one and at most 10,000 whole numbers (each from -10⁹ to 10⁹), separated by spaces and newlines.

Output

One line: SMALLEST LARGEST COUNT, where COUNT is how many times the largest number appears.

Constraints

1 ≤ count ≤ 10,000. Values fit in long long.

Example

Input:

4 9 -3 9 1

Output:

-3 9 2

Why it works

One pass is enough. When a number beats the largest so far, the count restarts at 1; when it equals the largest, the count goes up by 1. Starting smallest and largest from the first number avoids guessing a start value that might be wrong.

Examples

Example 1 · Input
4 9 -3 9 1
Expected output
-3 9 2
Example 2 · Input
7
Expected output
7 7 1

Hints

Hint 1

Read the first number separately, and set smallest and largest to it.

Hint 2

If x > largest, set largest = x and count = 1. If x == largest, add 1 to count.

Hint 3

The numbers can be negative, so do not start from 0.

Solution

Show one correct solution (try it yourself first)
#include <iostream>
using namespace std;

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    long long x, smallest, largest;
    int count = 1;
    cin >> x;
    smallest = largest = x;
    while (cin >> x) {
        if (x < smallest) smallest = x;
        if (x > largest) {
            largest = x;
            count = 1;
        } else if (x == largest) {
            count++;
        }
    }
    cout << smallest << " " << largest << " " << count << "\n";
    return 0;
}

▶ Open this solution in the compiler, then press Visualize to watch it run step by step

Lesson: Contests, judges and time limits →