Given an array with n integers, you need to find if there are triplets (i, j, k) which satisfies following conditions:
0 < i, i + 1 < j, j + 1 < k < n - 1
Sum of subarrays (0, i - 1), (i + 1, j - 1), (j + 1, k - 1) and (k + 1, n - 1) should be equal.
where we define that subarray (L, R) represents a slice of the original array starting from the element indexed L to the element indexed R.
Example:
Note:
1 <= n <= 2000.
Elements in the given array will be in range [-1,000,000, 1,000,000].
解法1:
这题又是2sum的一个变种。把一个数组分成加和相等的4份。trick在于如果遍历中间的点,那么左右那边变成找出是否存在切割点使得切割之后的两份的加和相等。
那么这一部分就可以用hashset来解决。存储可以等分的sum,然后在右边的部分寻找等分的时候看是否在左面出现过。
C++
Java