Practical PapersISCClass 12Computer Science

ISC Practical 2027: Local Minimum Cells in a Matrix – Algorithm and Java Program

Fully solved ISC Computer Science Practical 2027 matrix question: in an elevation matrix of order M × N, find every Local Minimum Cell (an interior cell strictly lower than its four neighbours) and the smallest one.

7 min read
ISC Practical 2027: Local Minimum Cells in a Matrix – Algorithm and Java Program

This is Question 1 of Sample Paper 4 from the specimen questions CISCE has released for the ISC Year 2027 Computer Science Practical (Paper 2). The concept area is Two-dimensional arrays. Below you will find the question, a plain-language explanation of the logic, an exam-ready algorithm, a fully documented Java program, the real output for every sample input, and tips for scoring the full 15 marks.

Every program on this page was compiled with JDK 21 and run on CISCE’s sample inputs, plus extra boundary inputs of the kind a visiting examiner will try.

The question

Each element of a matrix denotes the elevation of a platform. A platform is a Local Minimum Cell (LMC) if its elevation is strictly lower than the elevations of its four adjacent platforms (up, down, left and right). Only interior cells are eligible, because boundary cells do not have four neighbours.

Write a program to declare a matrix H[][] of order M × N, where 4 ≤ M ≤ 9 and 4 ≤ N ≤ 9 (check for validity, otherwise display INVALID INPUT). Accept integer heights and:

  1. Display the matrix.
  2. Display every Local Minimum Cell and its position.
  3. Find and display the smallest LMC. If there is none, display NO LOCAL MINIMUM CELL.

CISCE gives 4 examples with this question. Each one is shown with its output in the Output section below, so you can check your own program against it.

Understanding the problem

A double-dimensional array neighbour question, simpler than the bright-spot problem because boundary cells are excluded completely.

  • Interior cells only: loop i from 1 to M − 2 and j from 1 to N − 2. Every interior cell then has all four neighbours inside the array, so no bounds checks are needed.
  • Strictly lower means < for all four comparisons. In Example 3, the 7 at (2,3) has a neighbour equal to 7, so it is not an LMC.
  • Smallest LMC: start with Integer.MAX_VALUE and update it whenever an LMC is found. A boolean found decides between printing the smallest LMC and "NO LOCAL MINIMUM CELL".
  • Positions are printed from 1, for example 1 AT (2,2) for H[1][1].
Elevation matrix with the interior region outlined and local minimum cells highlighted
Question 10: worked example

Algorithm [3 marks]

Write the algorithm in the planning session, in numbered steps like these. It must match your program exactly.

  1. Start.
  2. Input M and N. If either is less than 4 or greater than 9, print "INVALID INPUT" and stop.
  3. Input M × N integers into H[][] row by row and display the matrix.
  4. Set found = false and smallest = a very large value.
  5. For i = 1 to M − 2 and j = 1 to N − 2: if H[i][j] is less than H[i − 1][j], H[i + 1][j], H[i][j − 1] and H[i][j + 1], print H[i][j] with the position (i + 1, j + 1), set found = true and update smallest.
  6. If found, print "SMALLEST LMC = " smallest; otherwise print "NO LOCAL MINIMUM CELL".
  7. Stop.

Java program [7 marks]

The program uses a class with separate methods for each task and comments that explain every data member and the special logic. That is what the 2 documentation marks are for.

LocalMinimum.java
import java.util.Scanner;

/**
 * ISC Practical 2027 - Sample Paper 4, Question 1
 * Finds the Local Minimum Cells (interior cells lower than all four neighbours)
 * of an elevation matrix H[M][N], 4 <= M, N <= 9.
 */
public class LocalMinimum
{
    int H[][];           // elevation matrix
    int M, N;            // number of rows and columns

    LocalMinimum(int m, int n)
    {
        M = m;
        N = n;
        H = new int[M][N];
    }

    void accept(Scanner sc)
    {
        System.out.println("Enter the numbers:");
        for (int i = 0; i < M; i++)
            for (int j = 0; j < N; j++)
                H[i][j] = sc.nextInt();
    }

    void display()
    {
        System.out.println("THE MATRIX IS:");
        for (int i = 0; i < M; i++)
        {
            for (int j = 0; j < N; j++)
                System.out.print(H[i][j] + " ");
            System.out.println();
        }
    }

    // true if H[i][j] is strictly lower than its up, down, left and right neighbours
    boolean isLMC(int i, int j)
    {
        int v = H[i][j];
        return v < H[i - 1][j] && v < H[i + 1][j] && v < H[i][j - 1] && v < H[i][j + 1];
    }

    void findLMC()
    {
        int smallest = Integer.MAX_VALUE;
        boolean found = false;
        for (int i = 1; i < M - 1; i++)              // only interior rows
            for (int j = 1; j < N - 1; j++)          // only interior columns
                if (isLMC(i, j))
                {
                    if (!found)
                        System.out.println("LOCAL MINIMUM CELL");
                    found = true;
                    System.out.println(H[i][j] + " AT (" + (i + 1) + "," + (j + 1) + ")");
                    if (H[i][j] < smallest)
                        smallest = H[i][j];
                }
        if (found)
            System.out.println("SMALLEST LMC = " + smallest);
        else
            System.out.println("NO LOCAL MINIMUM CELL");
    }

    public static void main(String args[])
    {
        Scanner sc = new Scanner(System.in);
        System.out.print("M = ");
        int m = sc.nextInt();
        System.out.print("N = ");
        int n = sc.nextInt();
        if (m < 4 || m > 9 || n < 4 || n > 9)
        {
            System.out.println("INVALID INPUT");
            return;
        }
        LocalMinimum ob = new LocalMinimum(m, n);
        ob.accept(sc);
        ob.display();
        ob.findLMC();
    }
}

Variable description

Variable description
Name           Type       Purpose
-------------  ---------  -------
H[][]          int[][]    elevation matrix
M, N           int        number of rows and columns
i, j           int        row and column of the cell being tested
found          boolean    true once an LMC is found
smallest       int        smallest LMC so far

Output [3 marks]

These are the actual outputs of the program above for every example in the question:

Example 1
INPUT:
M = 4
N = 4
Enter the numbers:
8 7 9 6 6 1 8 5 9 7 2 8 5 6 7 9

OUTPUT:
THE MATRIX IS:
8 7 9 6
6 1 8 5
9 7 2 8
5 6 7 9
LOCAL MINIMUM CELL
1 AT (2,2)
2 AT (3,3)
SMALLEST LMC = 1
Example 2
INPUT:
M = 5
N = 5
Enter the numbers:
9 9 9 9 9 9 5 7 4 9 9 8 3 8 9 9 6 7 5 9 9 9 9 9 9

OUTPUT:
THE MATRIX IS:
9 9 9 9 9
9 5 7 4 9
9 8 3 8 9
9 6 7 5 9
9 9 9 9 9
LOCAL MINIMUM CELL
5 AT (2,2)
4 AT (2,4)
3 AT (3,3)
6 AT (4,2)
5 AT (4,4)
SMALLEST LMC = 3
Example 3
INPUT:
M = 4
N = 4
Enter the numbers:
1 2 3 4 5 6 7 8 9 8 7 6 5 4 3 2

OUTPUT:
THE MATRIX IS:
1 2 3 4
5 6 7 8
9 8 7 6
5 4 3 2
NO LOCAL MINIMUM CELL
Example 4
INPUT:
M = 3
N = 12

OUTPUT:
INVALID INPUT

Test it on unknown inputs

One of the 3 output marks is for correct output on inputs the examiner chooses, usually boundary cases. Our program handles these correctly:

4 × 5 matrix: the 1 on the boundary row is ignored
INPUT:
M = 4
N = 5
Enter the numbers:
5 5 5 5 5 5 1 5 4 5 5 5 5 5 5 5 5 1 5 5

OUTPUT:
THE MATRIX IS:
5 5 5 5 5
5 1 5 4 5
5 5 5 5 5
5 5 1 5 5
LOCAL MINIMUM CELL
1 AT (2,2)
4 AT (2,4)
SMALLEST LMC = 1

How to score full marks on this question

Marks distribution for the ISC Computer Science practical: algorithm 3, program 7, hard copy 2, output 3
ISC Paper 2: how the 15 exam marks are divided
  • Write isLMC(i, j) as a boolean method. It keeps the nested loop short and readable.
  • Validate M and N before creating the array.
  • Test a non-square matrix (for example 4 × 5) to show your loops use M and N correctly.
  • Keep the typed program identical to the planned one. Large differences between your planning sheet and the printout cost the 2 hard-copy marks.

Common mistakes to avoid

  • Looping over the whole matrix and crashing at the edges.
  • Using <= so that a cell equal to a neighbour is counted.
  • Swapping M and N in the loop limits. This only shows up with a non-square matrix, which is exactly what an examiner may try.

This post is part of our series that solves all 12 specimen questions for the ISC 2027 Computer Science practical, one question a day. Practise writing the algorithm and program on paper in 90 minutes before you type it. That is exactly how the exam works.