Varargs

Lab Java Intermediate 20 points

Write static int max(int first, int... rest). Read lines of integers (first line is the number of lines) and print the maximum of each line by calling max with the first number and an array of the rest.

Input

n, then n lines of integers.

Output

n lines.

Example

Input

3
4 9 2
-5
1 1 7 3

Output

9
-5
7
Sample input available Sample output available
Editor
Java

Hints

int... rest is received as an int[]; you can also pass an int[] to it.

Solution explanation

Varargs let callers pass any number of arguments, like String.format does.

Show solution
import java.util.Scanner;

public class Main {
    static int max(int first, int... rest) {
        int best = first;
        for (int x : rest) {
            best = Math.max(best, x);
        }
        return best;
    }

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = Integer.parseInt(sc.nextLine().trim());
        for (int i = 0; i < n; i++) {
            String[] p = sc.nextLine().trim().split("\\s+");
            int[] rest = new int[p.length - 1];
            for (int j = 1; j < p.length; j++) {
                rest[j - 1] = Integer.parseInt(p[j]);
            }
            System.out.println(max(Integer.parseInt(p[0]), rest));
        }
    }
}
Output
Answer
HOME LEARN COMMUNITY DASHBOARD