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:
class Solution { public int solution(int N, int M, int[] X, int[] 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 also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public int solution(int N, int M, int[] X, int[] Y) {
// write your code in Java SE 8
if(X.length % 2 > 0) return 0;
else
{
}
}
}
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public int solution(int N, int M, int[] X, int[] Y) {
// write your code in Java SE 8
if(X.length % 2 > 0) return 0;
else
{
int med_
}
}
}
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public int solution(int N, int M, int[] X, int[] Y) {
// write your code in Java SE 8
if(X.length % 2 > 0) return 0;
else
{
int mid_x = X.length / 2;
int mid_y = Y.length / 2;
}
}
}
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public int solution(int N, int M, int[] X, int[] Y) {
// write your code in Java SE 8
if(X.length % 2 > 0) return 0;
else
{
int mid_x = X.length / 2;
int mid_y = Y.length / 2;
X = Arrays.
}
}
}
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public int solution(int N, int M, int[] X, int[] Y) {
// write your code in Java SE 8
if(X.length % 2 > 0) return 0;
else
{
int mid_x = X.length / 2;
int mid_y = Y.length / 2;
X = Arrays.sort(X);
Y = Arrays.sort(Y);
int ret =
}
}
}
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public int solution(int N, int M, int[] X, int[] Y) {
// write your code in Java SE 8
if(X.length % 2 > 0) return 0;
else
{
int mid_x = X.length / 2;
int mid_y = Y.length / 2;
X = Arrays.sort(X);
Y = Arrays.sort(Y);
int ret = (X[mid_x] - X[mid_x - 1]) +
}
}
}
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public int solution(int N, int M, int[] X, int[] Y) {
// write your code in Java SE 8
if(X.length % 2 > 0) return 0;
else
{
int mid_x = X.length / 2;
int mid_y = Y.length / 2;
X = Arrays.sort(X);
Y = Arrays.sort(Y);
int ret = (X[mid_x] - X[mid_x - 1]) + (Y[mid_y] - Y[mid_y - 1]);
return ret;
}
}
}
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public int solution(int N, int M, int[] X, int[] Y) {
// write your code in Java SE 8
if(X.length % 2 > 0) return 0;
else
{
int mid_x = X.length / 2;
int mid_y = Y.length / 2;
X = Arrays.sort(X);
Y = Arrays.sort(Y);
int ret = (X[mid_x] - X[mid_x - 1]) + (Y[mid_y] - Y[mid_y - 1]);
return ret;
}
}
}
Solution.java:16: error: cannot find symbol X = Arrays.sort(X); ^ symbol: variable Arrays location: class Solution Solution.java:17: error: cannot find symbol Y = Arrays.sort(Y); ^ symbol: variable Arrays location: class Solution 2 errors
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public int solution(int N, int M, int[] X, int[] Y) {
// write your code in Java SE 8
if(X.length % 2 > 0) return 0;
else
{
int mid_x = X.length / 2;
int mid_y = Y.length / 2;
Arrays.sort(X);
Y = Arrays.sort(Y);
int ret = (X[mid_x] - X[mid_x - 1]) + (Y[mid_y] - Y[mid_y - 1]);
return ret;
}
}
}
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public int solution(int N, int M, int[] X, int[] Y) {
// write your code in Java SE 8
if(X.length % 2 > 0) return 0;
else
{
int mid_x = X.length / 2;
int mid_y = Y.length / 2;
Arrays.sort(X);
Arrays.sort(Y);
int ret = (X[mid_x] - X[mid_x - 1]) + (Y[mid_y] - Y[mid_y - 1]);
return ret;
}
}
}
Solution.java:16: error: cannot find symbol Arrays.sort(X); ^ symbol: variable Arrays location: class Solution Solution.java:17: error: cannot find symbol Arrays.sort(Y); ^ symbol: variable Arrays location: class Solution 2 errors
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public int solution(int N, int M, int[] X, int[] Y) {
// write your code in Java SE 8
if(X.length % 2 > 0) return 0;
else
{
int mid_x = X.length / 2;
int mid_y = Y.length / 2;
Array.sort(X);
Array.sort(Y);
int ret = (X[mid_x] - X[mid_x - 1]) + (Y[mid_y] - Y[mid_y - 1]);
return ret;
}
}
}
Solution.java:16: error: cannot find symbol Array.sort(X); ^ symbol: variable Array location: class Solution Solution.java:17: error: cannot find symbol Array.sort(Y); ^ symbol: variable Array location: class Solution 2 errors
// you can also use imports, for example:
import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public int solution(int N, int M, int[] X, int[] Y) {
// write your code in Java SE 8
if(X.length % 2 > 0) return 0;
else
{
int mid_x = X.length / 2;
int mid_y = Y.length / 2;
Array.sort(X);
Array.sort(Y);
int ret = (X[mid_x] - X[mid_x - 1]) + (Y[mid_y] - Y[mid_y - 1]);
return ret;
}
}
}
// you can also use imports, for example:
import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public int solution(int N, int M, int[] X, int[] Y) {
// write your code in Java SE 8
if(X.length % 2 > 0) return 0;
else
{
int mid_x = X.length / 2;
int mid_y = Y.length / 2;
Arrays.sort(X);
Arrays.sort(Y);
int ret = (X[mid_x] - X[mid_x - 1]) + (Y[mid_y] - Y[mid_y - 1]);
return ret;
}
}
}
// you can also use imports, for example:
import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public int solution(int N, int M, int[] X, int[] Y) {
// write your code in Java SE 8
if(X.length % 2 > 0) return 0;
else
{
int mid_x = X.length / 2;
int mid_y = Y.length / 2;
Arrays.sort(X);
Arrays.sort(Y);
int ret = (X[mid_x] - X[mid_x - 1]) + (Y[mid_y] - Y[mid_y - 1]);
return ret;
}
}
}
// you can also use imports, for example:
import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public int solution(int N, int M, int[] X, int[] Y) {
// write your code in Java SE 8
if(X.length % 2 > 0) return 0;
else
{
int mid_x = X.length / 2;
int mid_y = Y.length / 2;
Arrays.sort(X);
Arrays.sort(Y);
int ret = (X[mid_x] - X[mid_x - 1]) + (Y[mid_y] - Y[mid_y - 1]);
return ret;
}
}
}
The solution obtained perfect score.