Find local minimum in n x n matrix in On time
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
Introduction
Finding a local minimum in a matrix is a classic problem that explores efficient search algorithms in computational theory. A local minimum in an matrix is an element that is smaller than or equal to all of its four neighboring elements (up, down, left, and right). The goal is to locate at least one such element using time complexity. This might seem counterintuitive at first, given the elements in the matrix, but it can be achieved through strategic algorithmic approaches.
Problem Definition
Consider an matrix where each element has integer values. A local minimum is defined as an element such that:
- (if )
- (if )
- (if )
- (if )
The challenge is to find such an element in time.
Approach
The method to find a local minimum in time hinges on a divide-and-conquer strategy. This approach capitalizes on selecting rows or columns strategically to minimize search space while still ensuring that a local minimum will be found. Below is a step-by-step breakdown of this approach using columns for simplicity:
Steps
- Select the Middle Column: Choose the middle column of the matrix. This step reduces the matrix to a simpler form.
- Find the Global Minimum in that Column: Identify the smallest element in the middle column. Assume this element is located at position . This operation is straightforward and takes time.
- Evaluate Adjacent Elements: Compare the selected element with its left and right neighbors (i.e., elements and ).
- If and , it is a local minimum.
- If , then the local minimum is in the left half of the matrix. Repeat the process on the submatrix formed by the left half.
- If , then the local minimum is in the right half of the matrix. Repeat the process on the submatrix formed by the right half.
- Recursive Reduction: The algorithm recursively concentrates on either half of the matrix at each step, constantly narrowing down the potential locations of a local minimum.
- Convergence: As the recursion narrows, it will ultimately converge upon a single row or column where the criteria for a local minimum are satisfied.
Time Complexity
Each recursive pass involves evaluating elements, leading to a time complexity of . Each division cuts the problem size by half. Thus, the recurrence relation for this operation can be framed as:
Solving this relation, we achieve an overall complexity of , capitalizing on efficient narrowing of search space rather than exhaustive search.
Example
Consider the matrix:
9 11 8 12
9 10 5 7
8 12 11 10
- Local minima on matrices can be applied in terrain modeling, grid-based simulations, and optimization problems where the adjacency condition applies.
- The approach can be modified for irregular matrices or matrices following different adjacency rules.
Related reading
- Find longest increasing sequence
- Find longest repetitive sequence in a string
- find lowest index of a given value in a presorted array
- Find maximum value in an array by recursion
- find median with minimum time in an array
- Find minimal Ai2 Bi2 when A and B are sorted
- Find median in binary search tree
- Find median value from a growing set

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.