Google TPS. Could only come up with brute-force 😭. Can you optimize?

Anonymous User
1785
Apr 10, 2025
Apr 10, 2025

Hi Guys.

Just faced google TPS round and messed up bad.

Problem statement:

Given an array of gold chain weights, you need to donate one of the weight and check if the remaining chain can be splitted into two equal halves.
Example: 5 2 4 2 7 Expected: True

Explaination:

  • Here you can donate the gold weight of 2 at index 1.
  • Remaining weights: 5 4 2 7
  • Here you can split this into two 5 4 and 2 7 with equal weights.

Man it was my golden chance to get into Google 😩.

Can anyone come up with an optimal solutions for this? I could only come with O(N^2) 😔

Update: I guess many of you are misunderstanding the actual problem. If I just write you in simple terms. You can donate any and only one of the weight and exclude it from your array and then check if its possible to split it at any point such that first-half and second-half sum is same.

Update 2: To all the coders who think you will calculate the prefixSum and suffixSum from the index of donation, this approach won't work. Let me explain the wrong understanding you guys having:

Suppose i is the index which you decided to donate. The you are assuming that you will check the prefix[0, i-1] and suffix[i+1, n-1].

Now this approach won't work and is wrong. Because even after donating the index i element, there can be possibility that split index is not i. This is clear even in my example testcase.

Now when using donations index is not valid. Then pre-computations will become invalid when we update something in an array. This needs re-computation which is again same as O(N^2).

Comments (44)