Image Overlap - Problem

Imagine you have two binary images represented as square matrices filled with 0s and 1s. Your task is to find the maximum overlap between these images by sliding one image over the other.

You can translate (slide) one image in any direction - left, right, up, or down - by any number of units. Think of it like placing a transparent sheet with holes (1s represent holes, 0s represent solid areas) over another sheet, and counting how many holes align perfectly.

Key Rules:

  • Only translation is allowed - no rotation
  • Any 1s that slide outside the matrix boundaries are lost
  • Overlap occurs when both images have a 1 at the same position
  • Return the maximum possible overlap count

This problem tests your ability to work with 2D transformations and efficiently explore all possible alignments.

Input & Output

example_1.py โ€” Basic Overlap
$ Input: A = [[1,1,0],[0,1,0],[0,1,0]], B = [[0,0,0],[0,1,1],[0,0,1]]
โ€บ Output: 3
๐Ÿ’ก Note: We translate image A by sliding it right by 1 unit and down by 1 unit. This aligns A's 1s at positions (0,0),(0,1),(1,1),(2,1) with B's 1s at positions (1,1),(1,2),(2,2), creating overlaps at (1,1), (1,2), and (2,2) for a total of 3.
example_2.py โ€” No Overlap Possible
$ Input: A = [[1]], B = [[1]]
โ€บ Output: 1
๐Ÿ’ก Note: Both matrices have a single 1 at position (0,0). With no translation (offset 0,0), they overlap perfectly with 1 matching position.
example_3.py โ€” Zero Matrix Edge Case
$ Input: A = [[0,0],[0,0]], B = [[1,1],[1,1]]
โ€บ Output: 0
๐Ÿ’ก Note: Matrix A has no 1s, so regardless of how we translate it, there can be no overlapping 1s. The maximum overlap is 0.

Visualization

Tap to expand
Transparency ATransparency BSlide & CountBest Alignment2 Stars Aligned!Key Insight: Only check offsets that align at least one pair of stars
Understanding the Visualization
1
Identify Star Positions
Mark all star cutout positions on both transparencies
2
Calculate Alignment Offsets
For each star on the first transparency, calculate how to align it with each star on the second
3
Count Offset Frequencies
Track which alignment offset would create the most star alignments
4
Find Optimal Translation
The most common offset gives the maximum number of aligned stars
Key Takeaway
๐ŸŽฏ Key Insight: Instead of trying all O(nยฒ) possible translations, focus only on the O(kยฒ) meaningful offsets that could align at least one pair of 1s, where k is the number of 1s in the matrices.

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(kยฒ)

k is the number of 1s in the matrices. We compare each 1 in A with each 1 in B

n
2n
โœ“ Linear Growth
Space Complexity
O(k)

Store coordinates of all 1s and frequency map of offsets

n
2n
โœ“ Linear Space

Constraints

  • n == img1.length == img1[i].length
  • n == img2.length == img2[i].length
  • 1 โ‰ค n โ‰ค 30
  • img1[i][j] is either 0 or 1
  • img2[i][j] is either 0 or 1
Asked in
Google 35 Microsoft 28 Facebook 22 Apple 15
28.5K Views
Medium Frequency
~18 min Avg. Time
892 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