Labeled break

Lab Java Intermediate 20 points

Read an r×c grid of integers and a target. Print the row and column of the first occurrence (reading row by row), or not found. Use a labeled break to exit both loops.

Input

r c, the grid, then the target.

Output

row col (0-based) or not found.

Example

Input

3 3
1 2 3
4 5 6
7 5 9
5

Output

1 1
Sample input available Sample output available
Editor
Java

Hints

Put a label like search: before the outer loop and write break search;

Solution explanation

A plain break only leaves the innermost loop; a labeled break leaves the named one.

Show solution
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int r = sc.nextInt(), c = sc.nextInt();
        int[][] grid = new int[r][c];
        for (int i = 0; i < r; i++) {
            for (int j = 0; j < c; j++) {
                grid[i][j] = sc.nextInt();
            }
        }
        int target = sc.nextInt();
        String result = "not found";
        search:
        for (int i = 0; i < r; i++) {
            for (int j = 0; j < c; j++) {
                if (grid[i][j] == target) {
                    result = i + " " + j;
                    break search;
                }
            }
        }
        System.out.println(result);
    }
}
Output
Answer
HOME LEARN COMMUNITY DASHBOARD