An old king wants to divide his kingdom between his two sons. He is well known for his justness and wisdom, and he plans to make a good use of both of these attributes while dividing his kingdom.
The kingdom is administratively split into square boroughs that form an N × M grid. Some of the boroughs contain gold mines. The king knows that his sons do not care as much about the land as they do about gold, so he wants both parts of the kingdom to contain exactly the same number of mines. Moreover, he wants to split the kingdom with either a horizontal or a vertical line that goes along the borders of the boroughs (splitting no borough into two parts).
The goal is to count how many ways he can split the kingdom.
Write a function:
def solution(N, M, X, Y)
that, given two arrays of K integers X and Y, denoting coordinates of boroughs containing the gold mines, will compute the number of fair divisions of the kingdom.
For example, given N = 5, M = 5, X = [0, 4, 2, 0] and Y = [0, 0, 1, 4], the function should return 3. The king can divide his land in three different ways shown on the picture below.

Write an efficient algorithm for the following assumptions:
- N and M are integers within the range [1..100,000];
- K is an integer within the range [1..100,000];
- each element of array X is an integer within the range [0..N-1];
- each element of array Y is an integer within the range [0..M-1].
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(N, M, X, Y):
# write your code in Python 3.6
cnt1 = 0;
cnt2 = 0;
# X = sorted(X);
# Y = sorted(Y);
if (M<1) | (N<1) | (N>100000) | (M>100000) | \
(len(X) % 2 != 0) | (max(X)>= N) | (max(Y) >= M) | (min(X)<0) | (min(Y) < 0) :
return 0
elif (M==1) & (N==1):
return 0
elif (M==1) & (N>=2):
for i in range(N):
y = [X[k] for k, x in enumerate(X) if x <= i]
y2 = [X[k] for k, x in enumerate(X) if x>i]
if(len(y) == len(y2)):
cnt1 = cnt1+1;
else:
cnt1 = cnt1;
return cnt1
elif (N==1) & (M>=2):
for i in range(M):
y = [Y[k] for k, x in enumerate(Y) if x <= i]
y2 =[Y[k] for k, x in enumerate(Y) if x >i]
if(len(y) == len(y2)):
cnt2 = cnt2+1;
else: cnt2 = cnt2
return cnt2
else:
for i in range(N):
y = [X[k] for k, x in enumerate(X) if x <= i]
y2 = [X[k] for k, x in enumerate(X) if x>i]
if(len(y) == len(y2)):
cnt1 = cnt1+1;
else:
cnt1 = cnt1;
for i in range(M):
y = [Y[k] for k, x in enumerate(Y) if x <= i]
y2 = [Y[k] for k, x in enumerate(Y) if x>i]
if(len(y) == len(y2)):
cnt2 = cnt2+1;
else:
cnt2 = cnt2
return cnt1 + cnt2
pass
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(N, M, X, Y):
# write your code in Python 3.6
cnt1 = 0;
cnt2 = 0;
# X = sorted(X);
# Y = sorted(Y);
if (M<1) | (N<1) | (N>100000) | (M>100000) | \
(len(X) % 2 != 0) | (max(X)>= N) | (max(Y) >= M) | (min(X)<0) | (min(Y) < 0) :
return 0
elif (M==1) & (N==1):
return 0
elif (M==1) & (N>=2):
for i in range(N):
y = [X[k] for k, x in enumerate(X) if x <= i]
y2 = [X[k] for k, x in enumerate(X) if x>i]
if(len(y) == len(y2)):
cnt1 = cnt1+1;
else:
cnt1 = cnt1;
return cnt1
elif (N==1) & (M>=2):
for i in range(M):
y = [Y[k] for k, x in enumerate(Y) if x <= i]
y2 =[Y[k] for k, x in enumerate(Y) if x >i]
if(len(y) == len(y2)):
cnt2 = cnt2+1;
else: cnt2 = cnt2
return cnt2
else:
for i in range(N):
y = [X[k] for k, x in enumerate(X) if x <= i]
y2 = [X[k] for k, x in enumerate(X) if x>i]
if(len(y) == len(y2)):
cnt1 = cnt1+1;
else:
cnt1 = cnt1;
for i in range(M):
y = [Y[k] for k, x in enumerate(Y) if x <= i]
y2 = [y]
if(len(y) == len(y2)):
cnt2 = cnt2+1;
else:
cnt2 = cnt2
return cnt1 + cnt2
pass
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(N, M, X, Y):
# write your code in Python 3.6
cnt1 = 0;
cnt2 = 0;
# X = sorted(X);
# Y = sorted(Y);
if (M<1) | (N<1) | (N>100000) | (M>100000) | \
(len(X) % 2 != 0) | (max(X)>= N) | (max(Y) >= M) | (min(X)<0) | (min(Y) < 0) :
return 0
elif (M==1) & (N==1):
return 0
elif (M==1) & (N>=2):
for i in range(N):
y = [X[k] for k, x in enumerate(X) if x <= i]
y2 = [X[k] for k, x in enumerate(X) if x>i]
if(len(y) == len(y2)):
cnt1 = cnt1+1;
else:
cnt1 = cnt1;
return cnt1
elif (N==1) & (M>=2):
for i in range(M):
y = [Y[k] for k, x in enumerate(Y) if x <= i]
y2 =[Y[k] for k, x in enumerate(Y) if x >i]
if(len(y) == len(y2)):
cnt2 = cnt2+1;
else: cnt2 = cnt2
return cnt2
else:
for i in range(N):
y = [X[k] for k, x in enumerate(X) if x <= i]
y2 = [X[k] for k, x in enumerate(X) if x>i]
if(len(y) == len(y2)):
cnt1 = cnt1+1;
else:
cnt1 = cnt1;
for i in range(M):
y = [Y[k] for k, x in enumerate(Y) if x <= i]
y2 = [k for k in Y if k not in y]
if(len(y) == len(y2)):
cnt2 = cnt2+1;
else:
cnt2 = cnt2
return cnt1 + cnt2
pass
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(N, M, X, Y):
# write your code in Python 3.6
cnt1 = 0;
cnt2 = 0;
# X = sorted(X);
# Y = sorted(Y);
if (M<1) | (N<1) | (N>100000) | (M>100000) | \
(len(X) % 2 != 0) | (max(X)>= N) | (max(Y) >= M) | (min(X)<0) | (min(Y) < 0) :
return 0
elif (M==1) & (N==1):
return 0
elif (M==1) & (N>=2):
for i in range(N):
y = [X[k] for k, x in enumerate(X) if x <= i]
y2 = [X[k] for k, x in enumerate(X) if x>i]
if(len(y) == len(y2)):
cnt1 = cnt1+1;
else:
cnt1 = cnt1;
return cnt1
elif (N==1) & (M>=2):
for i in range(M):
y = [Y[k] for k, x in enumerate(Y) if x <= i]
y2 =[Y[k] for k, x in enumerate(Y) if x >i]
if(len(y) == len(y2)):
cnt2 = cnt2+1;
else: cnt2 = cnt2
return cnt2
else:
for i in range(N):
y = [X[k] for k, x in enumerate(X) if x <= i]
y2 = [k for k in X if k not in y]
if(len(y) == len(y2)):
cnt1 = cnt1+1;
else:
cnt1 = cnt1;
for i in range(M):
y = [Y[k] for k, x in enumerate(Y) if x <= i]
y2 = [k for k in Y if k not in y]
if(len(y) == len(y2)):
cnt2 = cnt2+1;
else:
cnt2 = cnt2
return cnt1 + cnt2
pass
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(N, M, X, Y):
# write your code in Python 3.6
cnt1 = 0;
cnt2 = 0;
# X = sorted(X);
# Y = sorted(Y);
if (M<1) | (N<1) | (N>100000) | (M>100000) | \
(len(X) % 2 != 0) | (max(X)>= N) | (max(Y) >= M) | (min(X)<0) | (min(Y) < 0) :
return 0
elif (M==1) & (N==1):
return 0
elif (M==1) & (N>=2):
for i in range(N):
y = [X[k] for k, x in enumerate(X) if x <= i]
y2 = [k for k in X if k not in y]
if(len(y) == len(y2)):
cnt1 = cnt1+1;
else:
cnt1 = cnt1;
return cnt1
elif (N==1) & (M>=2):
for i in range(M):
y = [Y[k] for k, x in enumerate(Y) if x <= i]
y2 =[k for k in Y if k not in y]
if(len(y) == len(y2)):
cnt2 = cnt2+1;
else: cnt2 = cnt2
return cnt2
else:
for i in range(N):
y = [X[k] for k, x in enumerate(X) if x <= i]
y2 = [k for k in X if k not in y]
if(len(y) == len(y2)):
cnt1 = cnt1+1;
else:
cnt1 = cnt1;
for i in range(M):
y = [Y[k] for k, x in enumerate(Y) if x <= i]
y2 = [k for k in Y if k not in y]
if(len(y) == len(y2)):
cnt2 = cnt2+1;
else:
cnt2 = cnt2
return cnt1 + cnt2
pass
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(N, M, X, Y):
# write your code in Python 3.6
cnt1 = 0;
cnt2 = 0;
# X = sorted(X);
# Y = sorted(Y);
if (M<1) | (N<1) | (N>100000) | (M>100000) | \
(len(X) % 2 != 0) | (max(X)>= N) | (max(Y) >= M) | (min(X)<0) | (min(Y) < 0) :
return 0
elif (M==1) & (N==1):
return 0
elif (M==1) & (N>=2):
for i in range(N):
y = [X[k] for k, x in enumerate(X) if x <= i]
y2 = [k for k in X if k not in y]
if(len(y) == len(y2)):
cnt1 = cnt1+1;
else:
cnt1 = cnt1;
return cnt1
elif (N==1) & (M>=2):
for i in range(M):
y = [Y[k] for k, x in enumerate(Y) if x <= i]
y2 =[k for k in Y if k not in y]
if(len(y) == len(y2)):
cnt2 = cnt2+1;
else: cnt2 = cnt2
return cnt2
else:
for i in range(N):
y = [X[k] for k, x in enumerate(X) if x <= i]
y2 = [k for k in X if k not in y]
if(len(y) == len(y2)):
cnt1 = cnt1+1;
else:
cnt1 = cnt1;
for i in range(M):
y = [Y[k] for k, x in enumerate(Y) if x <= i]
y2 = [k for k in Y if k not in y]
if(len(y) == len(y2)):
cnt2 = cnt2+1;
else:
cnt2 = cnt2
return cnt1 + cnt2
pass
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(N, M, X, Y):
# write your code in Python 3.6
cnt1 = 0;
cnt2 = 0;
# X = sorted(X);
# Y = sorted(Y);
if (M<1) | (N<1) | (N>100000) | (M>100000) | \
(len(X) % 2 != 0) | (max(X)>= N) | (max(Y) >= M) | (min(X)<0) | (min(Y) < 0) :
return 0
elif (M==1) & (N==1):
return 0
elif (M==1) & (N>=2):
for i in range(N):
y = [X[k] for k, x in enumerate(X) if x <= i]
y2 = [k for k in X if k not in y]
if(len(y) == len(y2)):
cnt1 = cnt1+1;
else:
cnt1 = cnt1;
return cnt1
elif (N==1) & (M>=2):
for i in range(M):
y = [Y[k] for k, x in enumerate(Y) if x <= i]
y2 =[k for k in Y if k not in y]
if(len(y) == len(y2)):
cnt2 = cnt2+1;
else: cnt2 = cnt2
return cnt2
else:
for i in range(N):
y = [X[k] for k, x in enumerate(X) if x <= i]
y2 = [k for k in X if k not in y]
if(len(y) == len(y2)):
cnt1 = cnt1+1;
else:
cnt1 = cnt1;
for i in range(M):
y = [Y[k] for k, x in enumerate(Y) if x <= i]
y2 = [k for k in Y if k not in y]
if(len(y) == len(y2)):
cnt2 = cnt2+1;
else:
cnt2 = cnt2
return cnt1 + cnt2
pass
The following issues have been detected: timeout errors.
random tests with many rows or many columns
running time: 0.30 sec., time limit: 0.26 sec.
random tests with many rows and many columns
running time: 0.57 sec., time limit: 0.37 sec.