Program to find smallest intersecting element of each row in a matrix in Python

Suppose we have a 2D matrix where each row is sorted in ascending order. We have to find the smallest number that exists in every row. If there's no such result, then return −1.

So, if the input is like ?

2 3 5
5 10 10
1 3 5

then the output will be 5 because it's the smallest element present in all three rows.

Algorithm

To solve this, we will follow these steps ?

  • If matrix is empty, then return −1

  • Create a set from the first row of matrix

  • For each row in matrix:

    • Find intersection between current set and row elements

    • If intersection becomes empty, return −1

  • Return minimum element from the final intersection set

Using Set Intersection

This approach uses set operations to find common elements across all rows ?

class Solution:
    def solve(self, matrix):
        if not matrix:
            return -1
        
        # Start with elements from first row
        first = set(matrix[0])
        
        # Find intersection with each subsequent row
        for row in matrix:
            first &= set(row)
            if not first:
                return -1
        
        return min(first)

# Test the solution
ob1 = Solution()
matrix = [
    [2, 3, 5],
    [5, 10, 10],
    [1, 3, 5]
]
print(ob1.solve(matrix))
5

Alternative Approach Using Dictionary

We can also count occurrences of each element and find those present in all rows ?

def find_smallest_common_element(matrix):
    if not matrix:
        return -1
    
    rows = len(matrix)
    element_count = {}
    
    # Count occurrences of each element
    for row in matrix:
        unique_elements = set(row)  # Avoid counting duplicates in same row
        for element in unique_elements:
            element_count[element] = element_count.get(element, 0) + 1
    
    # Find elements present in all rows
    common_elements = [elem for elem, count in element_count.items() if count == rows]
    
    return min(common_elements) if common_elements else -1

# Test with example matrix
matrix = [
    [2, 3, 5],
    [5, 10, 10],
    [1, 3, 5]
]
print(find_smallest_common_element(matrix))
5

Edge Cases

Let's test with cases where no common element exists ?

# No common elements
matrix1 = [
    [1, 2, 3],
    [4, 5, 6],
    [7, 8, 9]
]

# Empty matrix
matrix2 = []

# Single row
matrix3 = [[1, 3, 5]]

ob = Solution()
print("No common elements:", ob.solve(matrix1))
print("Empty matrix:", ob.solve(matrix2))
print("Single row:", ob.solve(matrix3))
No common elements: -1
Empty matrix: -1
Single row: 1

Comparison

Method Time Complexity Space Complexity Best For
Set Intersection O(m × n) O(n) Clean and efficient
Dictionary Counting O(m × n) O(k) When tracking frequencies needed

Where m is number of rows, n is average row length, and k is number of unique elements.

Conclusion

The set intersection approach provides an elegant solution to find the smallest common element across all matrix rows. Use the dictionary method when you need additional frequency information or want to avoid set operations.

Updated on: 2026-03-25T11:38:38+05:30

229 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements