Matrix Pattern Challenges in Python (With Optimized Solutions!)
Matrices can hide surprisingly elegant patterns—especially when paired with recursion-style thinking and efficient algorithms. In this post, we'll explore two fun matrix problems that test your logic and help you master matrix traversal techniques! Problem One: Count Values Less Than Target Problem You’re given a matrix where: Each row is sorted in ascending order Each column is also sorted in ascending order Your task? Write a function that counts how many elements are smaller than a given target. And yes—you need to do this in O(n + m) time. Strategy Start from the top-right corner. If the current value is less than the target, everything to the left in that row is also smaller. So, count them all and move down. If the current value is greater than or equal to the target, move left to find smaller numbers. Keep doing this until you fall off the matrix. Python Solution def count_less_than(matrix, target): if not matrix or not matrix[0]: return 0 n = len(matrix) m = len(matrix[0]) count = 0 row = 0 col = m - 1 while row = 0: if matrix[row][col]

Matrices can hide surprisingly elegant patterns—especially when paired with recursion-style thinking and efficient algorithms. In this post, we'll explore two fun matrix problems that test your logic and help you master matrix traversal techniques!
Problem One: Count Values Less Than Target
Problem
You’re given a matrix where:
- Each row is sorted in ascending order
- Each column is also sorted in ascending order
Your task? Write a function that counts how many elements are smaller than a given target. And yes—you need to do this in O(n + m) time.
Strategy
- Start from the top-right corner.
- If the current value is less than the target, everything to the left in that row is also smaller. So, count them all and move down.
- If the current value is greater than or equal to the target, move left to find smaller numbers.
- Keep doing this until you fall off the matrix.
Python Solution
def count_less_than(matrix, target):
if not matrix or not matrix[0]:
return 0
n = len(matrix)
m = len(matrix[0])
count = 0
row = 0
col = m - 1
while row < n and col >= 0:
if matrix[row][col] < target:
count += (col + 1)
row += 1
else:
col -= 1
return count
Test It!
matrix = [
[1, 2, 3, 4],
[2, 3, 4, 5],
[3, 4, 5, 6],
[4, 5, 6, 7]
]
print(count_less_than(matrix, 5)) # Output: 10
This counts all the values less than 5, giving you the result 10
.
Problem Two: Min & Max of the Secondary Diagonal
Problem
Given a square matrix, find the minimum and maximum values along the secondary diagonal, which goes from the top-right to bottom-left.
Example:
Secondary Diagonal
→ 3
→ 6
→ 9
Strategy
- If the matrix is empty, return
[None, None]
. - For each
i
from0
ton-1
, the secondary diagonal element is at index[i][n - 1 - i]
. - Track the min and max values as you traverse.
Python Solution
def solution(grid):
if not grid:
return [None, None]
n = len(grid)
if n == 0:
return [None, None]
min_value = float('inf')
max_value = float('-inf')
for i in range(n):
secondary_diagonal_value = grid[i][n - 1 - i]
min_value = min(min_value, secondary_diagonal_value)
max_value = max(max_value, secondary_diagonal_value)
return [min_value, max_value]
Test It!
grid = [
[1, 2, 3],
[4, 5, 6],
[7, 8, 9]
]
print(solution(grid)) # Output: [3, 7]
Recap
- The first problem uses greedy scanning from the top-right to quickly count qualifying values.
- The second one is a diagonal extraction that leverages fixed index logic for a fast O(n) solution.
These are great examples of how sorted structure + smart traversal leads to high-performance solutions.