Part A: findPosition

Write a static method that searches a 2D integer array for a given value and returns its Position (row, column), or null if the value is not present.

class Position {
    private int row;
    private int col;

    public Position(int r, int c) {
        row = r;
        col = c;
    }

    public int getRow() { return row; }
    public int getCol() { return col; }

    @Override
    public String toString() {
        return "row=" + row + ", col=" + col;
    }
}

class Successors {
    public static Position findPosition(int num, int[][] intArr) {
        for (int r = 0; r < intArr.length; r++) {
            for (int c = 0; c < intArr[r].length; c++) {
                if (intArr[r][c] == num) {
                    return new Position(r, c);
                }
            }
        }
        return null;
    }
}

public class PartA {
    public static void main(String[] args) {
        int[][] arr = {
            {15,  5,  9, 10},
            {12, 16, 11,  6},
            {14,  8, 13,  7}
        };

        Position p1 = Successors.findPosition(8, arr);
        System.out.println("Position of 8: " + (p1 != null ? p1.toString() : "null"));

        Position p2 = Successors.findPosition(17, arr);
        System.out.println("Position of 17: " + (p2 != null ? p2.toString() : "null"));

        Position p3 = Successors.findPosition(15, arr);
        System.out.println("Position of 15: " + (p3 != null ? p3.toString() : "null"));
    }
}
PartA.main(null);
Position of 8: row=2, col=1
Position of 17: null
Position of 15: row=0, col=0

The nested loops scan row by row, left to right. The moment intArr[r][c] equals num, a Position is immediately returned. If the loops complete without a match, null is returned — covering the case where the value simply does not appear in the array.


Part B: getSuccessorArray

Write a static method that builds and returns a 2D array of Position objects with the same dimensions as the input. Each cell (r, c) holds the position of intArr[r][c] + 1 (the successor), or null if that successor value is not in the array.

class Position {
    private int row;
    private int col;

    public Position(int r, int c) {
        row = r;
        col = c;
    }

    public int getRow() { return row; }
    public int getCol() { return col; }

    @Override
    public String toString() {
        return "(" + row + "," + col + ")";
    }
}

class Successors {
    public static Position findPosition(int num, int[][] intArr) {
        for (int r = 0; r < intArr.length; r++) {
            for (int c = 0; c < intArr[r].length; c++) {
                if (intArr[r][c] == num) {
                    return new Position(r, c);
                }
            }
        }
        return null;
    }

    public static Position[][] getSuccessorArray(int[][] intArr) {
        Position[][] result = new Position[intArr.length][intArr[0].length];
        for (int r = 0; r < intArr.length; r++) {
            for (int c = 0; c < intArr[r].length; c++) {
                result[r][c] = findPosition(intArr[r][c] + 1, intArr);
            }
        }
        return result;
    }
}

public class PartB {
    public static void main(String[] args) {
        int[][] arr = {
            {15,  5,  9, 10},
            {12, 16, 11,  6},
            {14,  8, 13,  7}
        };

        Position[][] successors = Successors.getSuccessorArray(arr);

        System.out.println("Successor array:");
        for (Position[] row : successors) {
            for (Position p : row) {
                System.out.printf("%-7s", p != null ? p.toString() : "null");
            }
            System.out.println();
        }
    }
}
PartB.main(null);
Successor array:
(1,1)  (1,3)  (0,3)  (1,2)  
(1,0)  null   (1,0)  (2,3)  
(0,0)  (0,2)  (2,0)  (2,1)  

Key Takeaways

  • Helper method reuse: getSuccessorArray calls findPosition for every cell rather than reimplementing the search. Delegating to the helper keeps the logic clean and matches the problem’s intent.
  • Null propagation: findPosition returns null when a value is absent. getSuccessorArray stores that null directly in the result array — no extra null-check is needed before assigning.
  • Array dimensions: The result array is created with new Position[intArr.length][intArr[0].length] to exactly mirror the input dimensions. Using a hardcoded size would lose generality.
  • Successor definition: The successor of intArr[r][c] is intArr[r][c] + 1 — a consecutive integer value, not the adjacent cell. The search scans the entire 2D array for that value, regardless of where it sits.
  • Nested loop traversal: Both methods use the same row-then-column nested loop pattern. Consistent traversal order ensures every element is visited exactly once.