Skip to content

Nearby Squares (1)

Course: Recursion - chapter 2

Link: https://www.hackerearth.com/practice/basic-programming/recursion/recursion-and-backtracking/practice-problems/algorithm/nearby-squares-338a4a64/

What can be the states? For each element, we need to decide if it goes into array B or C.

So essentially, we need 3 things: the current element we are at, the sum of B achieved till now, and the sum of C achieved till now. So the function can be something like

f(i, x, y) = MIN(f(i+1, x + arr[I], y), f(i+1, x, y+arr[i]))

The base case will be when we are done with all elements, we need to report the absolute difference

f(n, x, y) = abs(x - y)
int diff(int i, int x, int y, int arr[], int n) {
if (i == n) return abs(x - y);
return diff(i+1, x + arr[i], y, arr, n) + diff(i+1, x, y + arr[i], arr, n);
}

Can we optimize this further?

If you think about this, the term y is kind of irrelevant. Because we can always derive y at the end using y = total sum - x

So, instead, we can write it like this:

f(i, x) = MIN(f(i+1, x), f(i+1, x+arr[i])

and base case becomes: f(n, x) = sum - x

int diff(int i, int x, int arr[], int n, int sum) {
if (i == n) return abs(x - (sum - x));
return diff(i+1, x + arr[i], arr, n, sum) + diff(i+1, x, arr, n, sum);
}

Link: https://www.hackerearth.com/practice/basic-programming/recursion/recursion-and-backtracking/practice-problems/algorithm/nearby-squares-338a4a64/

What can be the states? For each element, we need to decide if it goes into array B or C.

So essentially, we need 3 things: the current element we are at, the sum of B achieved till now, and the sum of C achieved till now. So the function can be something like

f(i, x, y) = MIN(f(i+1, x + arr[I], y), f(i+1, x, y+arr[i]))

The base case will be when we are done with all elements, we need to report the absolute difference

f(n, x, y) = abs(x*x - y*y)
int diff(int i, int x, int y, int arr[], int n) {
if (i == n) return abs(x*x - y*y);
return diff(i+1, x + arr[i], y, arr, n) + diff(i+1, x, y + arr[i], arr, n);
}

Can we optimize this further?

If you think about this, the term y is kind of irrelevant. Because we can always derive y at the end using y = total sum - x

So, instead, we can write it like this:

f(i, x) = MIN(f(i+1, x), f(i+1, x+arr[i])

and base case becomes: f(n, x) = sum - x

int diff(int i, int x, int arr[], int n, int sum) {
if (i == n) return abs(x*x - (sum - x) * (sum - x));
return diff(i+1, x + arr[i], arr, n, sum) + diff(i+1, x, arr, n, sum);
}

As A[i] ≤ 10^8, the above will not work. But we can see N≤20, so 2^N is a valid complexity. For each element, we can have 2 choices - either A or B. So all possible choice combinations are 2^N.

For each element, we need to maintain 2 choices: 0 means it goes to A and 1 means it goes to B. So we get an array like this: [0, 1, 1, 0]

Now when we are done with all elements, we can iterate over this list and find the sum for A and B and then the difference of their squares.

https://p.ip.fi/vUmt