Image Overlap LeetCode

In the world of computer vision and image processing, the challenge of Image Overlap is intriguing. It identifies common regions between two distinct images. The LeetCode Image Overlap problem mirrors real-world scenarios where precise alignment is vital, such as in medical imaging and satellite analysis.

Key takeaways:

  • Understand what image overlap is with the help of clear illustrations.

  • Learn how to solve the LeetCode Image Overlap problem by shifting one matrix over another to maximize the overlap of 11’s.

  • Grasp the core idea of systematically shifting matrices to explore all possible overlaps and find the optimal solution.

  • See a step-by-step Python solution to implement the algorithm, including a clear explanation of each part.

  • Gain insights into the complexity analysis to understand the trade-offs in terms of computational efficiency.

Educative 99 helps you solve complex coding problems, such as LeetCode image overlap, by teaching you to recognize patterns and apply the right algorithms.

What is image overlap?

Image overlap refers to the area where two images or matrices align and share common pixels or values. In the context of coding problems like LeetCode’s “Image Overlap,” it involves shifting one matrix over another to find the position where the maximum number of corresponding 11’s overlap, helping to solve problems related to image recognition and alignment.

Let’s consider a scenario to understand image overlap:

This widget is not supported in dev-mode. Kindly enable it or run using yarn webapp:dev-widgets.

This visual example lays the foundation for the concepts we’ll explore further in the problem statement.

Problem statement

We are given two square binary matrices, imageA and imageB, both of size n×nn \times n . Our task is to find the maximum overlap between them by shifting either imageA or imageB up, down, left, or right. The overlap is the count of positions where both matrices have a 11.

Some important points that need to be remembered while solving this problem:

  1. We can slide either imageA or imageB, but not both, to find the maximum overlap.

  2. The sliding operation doesn’t involve rotating the images, only shifting them horizontally or vertically.

  3. If a 11 in one of the images is shifted outside the matrix boundary during the sliding operation, it is considered erased and does not contribute to the overlap.

Our task is to determine the largest possible overlap that can be achieved between imageA and imageB under these constraints. The result should be an integer representing the maximum overlap. Now, let’s map our previous example to the problem for better understanding.


This widget is not supported in dev-mode. Kindly enable it or run using yarn webapp:dev-widgets.
Note: In our current case, we found the best overlap in a single image translation. However, in different scenarios, we might need different translations (up, down, left, right) to achieve the maximum overlap in an image.

Knowledge test

Now that we have understood the problem, let’s check our knowledge by finding the maximum overlap of the example below:

This widget is not supported in dev-mode. Kindly enable it or run using yarn webapp:dev-widgets.

This widget is not supported in dev-mode. Kindly enable it or run using yarn webapp:dev-widgets.

Intuition

The Image Overlap problem can be thought of as sliding one binary matrix over another in different directions to find the position where the most 11’s overlap. For each shift, we count how many 11’s align between the two matrices. The goal is to try all possible shifts and find the one with the highest number of overlapping 11’s, which gives the maximum overlap.

Algorithm

Now that we’ve understood the problem, let’s understand it programmatically. The idea is to slide one matrix over the other and look for all possible overlaps in the up, down, left, and right directions.

Here’s how the algorithm works:

  1. We start sliding imageA on the imageB in a loop.

    1. We start with the index [0][0][0][0] of the matrix.

    2. Within this loop, we run a nested loop to check for the overlap in each iteration.

      1. In this nested loop, we count the ones in the current overlapped area.

      2. If the count of overlapping ones is greater than the previous counts, we update the maximum overlaps.

This widget is not supported in dev-mode. Kindly enable it or run using yarn webapp:dev-widgets.

Note: In the context of this algorithm, we don't introduce any extra 0's when determining the overlap between two images. Instead, we slide one image over the other to identify the maximum overlap between images A and B.

Solution implementation

Let’s have a look at the code for the algorithm we just discussed:

This widget is not supported in dev-mode. Kindly enable it or run using yarn webapp:dev-widgets.

Code explanation

  • Line 2: The find_max_overlap function slides one position up, down, left, and right and finds the maximum number of 1’s overlapping in the given arrangement.

  • Lines 11–27: In the nested for loops, we iterate through the elements of matrices A and B to check for overlapping 1’s. We use four variables—overlap_right_down, overlap_right_up, overlap_left_down, and overlap_left_up—to keep track of the count of overlapping 1’s in different directions: right-down, right-up, left-down, and left-up.

  • Line 14: We calculate the overlap in the right-down direction by checking if we can move one matrix A down and to the right without going out of bounds. If so, we increment the count based on the overlapping values of A and B. The same translations are performed for other directions in lines 18–27.

  • Line 30: The find_max_overlap function returns the maximum overlap among the four directions by finding the maximum value among overlap_right_down, overlap_left_down, overlap_right_up, and overlap_left_up.

  • Line 33: The largestOverlap function iterates over the entire matrix, considering different starting positions for A and B to calculate the maximum overlap in all possible arrangements. The maximum overlap is stored in the max_overlap variable and returned at the end.

Time complexity

The time complexity of the solution is O(n4)O(n^4). where n×nn \times n represents the size of square matrices A and B. This is due to the presence of four nested loops, with two in the find_max_overlap function and two in the largestOverlap function. In each iteration of these loops, calculations depend on the size of the matrices, leading to a quartic time complexity concerning n.

Space complexity

The space complexity of the provided solution is constant, denoted as O(1)O(1). The algorithm uses a fixed amount of additional space for variables and loop counters, regardless of the size of the input matrices. It does not create any data structures proportional to the input size, resulting in a constant space complexity.

To practice more LeetCode coding problems similar to Image Overlap, check out the best-selling Grokking the Coding Interview Patterns series. One of the standout features of this series is that it’s offered in six different programming languages.

This widget is not supported in dev-mode. Kindly enable it or run using yarn webapp:dev-widgets.

Free Resources

Copyright ©2026 Educative, Inc. All rights reserved