Jagged Arrays

Lab Java Intermediate 20 points

Read n and then n rows, each starting with its length followed by that many numbers. Store them in a jagged array (int[n][]) and print each row's length and maximum.

Input

n, then n lines: len v1 v2 ...

Output

n lines: len max

Example

Input

3
2 5 1
4 1 2 9 3
1 -7

Output

2 5
4 9
1 -7
Sample input available Sample output available
Editor
Java

Hints

new int[n][] creates n rows with no columns yet; create each row separately.

Solution explanation

Java's 2D arrays are arrays of arrays, so rows can have different lengths.

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[][] rows = new int[n][];
        for (int i = 0; i < n; i++) {
            rows[i] = new int[sc.nextInt()];
            for (int j = 0; j < rows[i].length; j++) {
                rows[i][j] = sc.nextInt();
            }
        }
        for (int[] row : rows) {
            int max = row[0];
            for (int v : row) {
                max = Math.max(max, v);
            }
            System.out.println(row.length + " " + max);
        }
    }
}
Output
Answer
HOME LEARN COMMUNITY DASHBOARD