Saddle Point Finder - Problem

A saddle point in a matrix is an element that is simultaneously the minimum in its row and the maximum in its column.

Given a 2D matrix, find all saddle points and return their coordinates as an array of [row, col] pairs.

Note: If no saddle points exist, return an empty array. Matrix elements can be positive, negative, or zero.

Input & Output

Example 1 — Matrix with One Saddle Point
$ Input: matrix = [[1,7,3],[9,11,5],[4,3,2]]
Output: [[2,2]]
💡 Note: Element at [2,2] is 2. It's the minimum in row 2: [4,3,2] and maximum in column 2: [3,5,2]. Therefore, it's a saddle point.
Example 2 — Matrix with No Saddle Points
$ Input: matrix = [[1,2],[3,4]]
Output: []
💡 Note: No element satisfies both conditions. Element 1 is row min but not column max, element 4 is column max but not row min.
Example 3 — Single Element Matrix
$ Input: matrix = [[5]]
Output: [[0,0]]
💡 Note: Single element 5 is both minimum in its row and maximum in its column by definition.

Constraints

  • 1 ≤ m, n ≤ 300
  • -104 ≤ matrix[i][j] ≤ 104

Visualization

Tap to expand
Saddle Point Finder - Matrix AnalysisINPUT MATRIXALGORITHMRESULT17391154323×3 Matrix with element 2highlighted at position [2,2]1Calculate row minimums2Calculate column maximums3Check each element againstprecomputed min/max4Collect saddle pointsRow mins: [1, 5, 2]Col maxs: [9, 11, 5]Element 2 = row min[2] ≠ col max[2]Saddle Points Found[[2,2]]Position [2,2] containsvalue 2 (saddle point)Element at [2,2] = 2Min in row & Max in columnKey Insight:Pre-calculating row minimums and column maximums eliminates redundantscanning, reducing time complexity from O(m×n×(m+n)) to O(m×n)TutorialsPoint - Saddle Point Finder | Optimized Preprocessing Approach
Asked in
Google 25 Microsoft 18 Amazon 15
28.5K Views
Medium Frequency
~15 min Avg. Time
842 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen