# Count overlaps in arrays:
def find_max_overlap(row, col, A, B):
# Initialize variables to store an overlap in different directions
overlap_right_down = 0
overlap_right_up = 0
overlap_left_down = 0
overlap_left_up = 0
n = len(A) # Assuming A and B are square matrices of size n x n
for i in range(n):
for j in range(n):
# Calculate overlap in the right-down direction
if i + row < n and j + col < n:
overlap_right_down += A[i + row][j + col] & B[i][j]
# Calculate overlap in the left-down direction
if i - row >= 0 and j + col < n:
overlap_left_down += A[i - row][j + col] & B[i][j]
# Calculate overlap in the left-up direction
if i - row >= 0 and j - col >= 0:
overlap_left_up += A[i - row][j - col] & B[i][j]
# Calculate overlap in the right-up direction
if j - col >= 0 and i + row < n:
overlap_right_up += A[i + row][j - col] & B[i][j]
# Return the maximum overlap among the four directions
return max(overlap_right_down, overlap_left_down, overlap_right_up, overlap_left_up)
def largestOverlap(A,B):
max_overlap = 0
# slides over the whole matrix in loops
for i in range(len(A)):
for j in range(len(A)):
# calculate and update the maximum overlap in current iteration
max_overlap = max(max_overlap, find_max_overlap(i, j, A, B))
return max_overlap
# ---------------------------------------------------------------
A = [[0,0,0],
[0,1,1],
[0,0,0]]
B = [[0,0,0],
[1,1,0],
[0,1,0]]
print("The largest overlap is :",largestOverlap(A,B))