Split Array With Equal Sums
You are given an array/list 'ARR' of size 'N'. You task is to find if there exists a triplet (i, j, k) such that 0 < i , i + 1 < j , j + 1 < k and k < 'N' - 1 and the sum of the subarrays [0, i - 1],[i + 1, j - 1], [j + 1, k - 1], [k + 1, N - 1] are equal.
An array c is a subarray of array d if c can be obtained from d by deletion of several elements from the beginning and several elements from the end.
Example:
let 'ARR' = [1, 2, 3] then the possible subarrays of 'ARR' will be - {1}, {2}, {3}, {1, 2}, {2, 3}, {1, 2, 3}.
Note: Assume That the Array has Zero-based indexing.
Input format:
The first line of input contains an integer ‘T’ denoting the number of test cases to run. Then the test case follows.
The first line of each test case contains an integer ‘N’ representing the array’s size.
The second line of each test case contains 'N' space-separated integers representing the array’s elements.
Output format:
For each test case, print ‘True’ if such a triplet exists; else print ‘False’.
Output for each test case will be printed in a separate line.
Note:
You don’t have to take any input or print anything; it already has been taken care of. Just implement the function.
Constraints:
1 <= T <= 5
1 <= N <= 10 ^ 3
-10 ^ 6 <= ARR[i] <= 10 ^ 6
Time Limit: 1 sec
CodingNinjas
author
2y
Brute Force
To implement this approach, You have to understand the constraints first, i.e., 0 < i, i+1 < j , j+1< k < N-1.
If you carefully look at the constraints, you may notice the problem while spli...read more
CodingNinjas
author
2y
Using Prefix Sum
This one is the optimization to the Brute force approach. We will iterate over all possible ‘i’ , ‘j’ and ‘k’ and for each possible subarray instead of calculating it’s sum in O(N) tim...read more
CodingNinjas
author
2y
Using Unordered Set
You can do this by first dividing the arraylist in two equal sums and then further dividing the first half in two equal sums and then checking the remaining half for these sums.
- Ini...read more
Add answer anonymously...
Popular interview questions of Software Developer Intern
>
Think Future Technologies Software Developer Intern Interview Questions
Stay ahead in your career. Get AmbitionBox app
Helping over 1 Crore job seekers every month in choosing their right fit company
65 L+
Reviews
4 L+
Interviews
4 Cr+
Salaries
1 Cr+
Users/Month
Contribute to help millions
Get AmbitionBox app