Skip to main content
๐Ÿ“œ WAYPOINT LESSON

Positions and neighbors

โญโญ intermediateโณ 12 min read๐Ÿ“ Lesson 110 of 256

Double early exit vs break, and orthogonal neighbor guards.

Two scanning shapes beyond the basics:

Find a position, return coordinates. When the answer is a place, return the row (or a coded r/c pair) โ€” and decide what happens when absent (โˆ’1 is conventional):

public static int findRow(int[][] g, int target) {
    for (int r = 0; r < g.length; r++) {
        for (int c = 0; c < g[r].length; c++) {
            if (g[r][c] == target) {
                return r;
            }
        }
    }
    return -1;
}

Note the double early-exit: the inner return leaves both loops at once. A break would only leave the inner loop โ€” a classic MCQ distinction.

Neighbors of g[r][c] โ€” up to four orthogonal friends, but edges have fewer. Guard each direction independently:

if (r > 0) { ... g[r - 1][c] ... }
if (r < g.length - 1) { ... g[r + 1][c] ... }
if (c > 0) { ... g[r][c - 1] ... }
if (c < g[r].length - 1) { ... g[r][c + 1] ... }

Or count matching neighbors:

int count = 0;
if (r > 0 && g[r - 1][c] == v) { count++; }
if (r < g.length - 1 && g[r + 1][c] == v) { count++; }
if (c > 0 && g[r][c - 1] == v) { count++; }
if (c < g[r].length - 1 && g[r][c + 1] == v) { count++; }

Never index g[r-1] before checking r > 0 โ€” the negative index throws before the value is even read.