C: Largest and smallest

Read n numbers and print the largest and the smallest, separated by a space.

What to build

A loop that remembers the best values so far.

Requirements

  • Read the numbers into an array.
  • Print the largest and then the smallest.

Input

The first line has n (1 ≤ n ≤ 100). The second line has n whole numbers between -1000000 and 1000000.

Output

largest smallest

Constraints

n is from 1 to 100.

Example

Input:

5
3 9 2 7 4

Output:

9 2

Why it works

Starting both from the first element, rather than from 0, gives correct answers even when all the numbers are negative.

Examples

Example 1 · Input
5
3 9 2 7 4
Expected output
9 2
Example 2 · Input
1
42
Expected output
42 42

Hints

Hint 1

Read the numbers into an array with a loop.

Hint 2

Start max and min at the first element, not at 0: all the numbers might be negative.

Solution

Show one correct solution (try it yourself first)
#include <stdio.h>

int main(void) {
    int n, a[100];
    scanf("%d", &n);
    for (int i = 0; i < n; i++) scanf("%d", &a[i]);
    int max = a[0], min = a[0];
    for (int i = 1; i < n; i++) {
        if (a[i] > max) max = a[i];
        if (a[i] < min) min = a[i];
    }
    printf("%d %d\n", max, min);
    return 0;
}

Lesson: Arrays →