Min, Max, Sum
Completed
Read n integers and print the minimum, maximum and sum, one per line.
Input
Line 1: n. Line 2: n integers.
Output
Three lines.
Example
Input
5 3 9 -2 7 1
Output
-2 9 18
Editor
Java
Hints
Start min and max at the first element, not at 0.
Solution explanation
The enhanced for (int x : a) loop visits every element without an index.
Show solution
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int[] a = new int[n];
for (int i = 0; i < n; i++) {
a[i] = sc.nextInt();
}
int min = a[0], max = a[0];
long sum = 0;
for (int x : a) {
min = Math.min(min, x);
max = Math.max(max, x);
sum += x;
}
System.out.println(min);
System.out.println(max);
System.out.println(sum);
}
}