Given an array of n integers, we have to partition the array into three segments such that all the segments have equal sum. Segment sum is the sum of all the elements in segment.
Input : 1, 3, 6, 2, 7, 1, 2, 8 Output : [1, 3, 6], [2, 7, 1], [2, 8] Input : 7, 6, 1, 7 Output : , [6, 1],  Input : 7, 6, 2, 7 Output : Cannot divide the array into segments
A simple solution is to consider all pairs of indexes and for every pair check if it divides array into three equal parts. If yes, then return true. Time complexity of this solution is O(n2)
An efficient approach is to use two auxiliary arrays and store the prefix and suffix array sum in these arrays respectively. We then use the two pointer approach, with variable ‘i’ pointing to starting of prefix array and variable ‘j’ pointing to the end of suffix array. If pre[i] > suf[j], then decrement ‘j’, otherwise increment ‘i’.
We maintain a variable whose value is the total sum of the array and whenever we encounter pre[i] = total_sum / 3 or suf[j] = total_sum / 3, we store the value of i or j respectively as segment boundaries.
First Segment : 1 3 6 Second Segment : 2 7 1 Third Segment : 2 8
Time Complexity : O(n)
Auxiliary Space : O(n)
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Partition an array such into maximum increasing segments
- Maximum sum of the array after dividing it into three segments
- Partition the array in K segments such that bitwise AND of individual segment sum is maximized
- Check if a large number can be divided into two or more segments of equal sum
- Partition of a set into K subsets with equal sum using BitMask and DP
- Partition an array of non-negative integers into two subsets such that average of both the subsets is equal
- Count the number of ways to divide an array into three contiguous parts having equal sum
- Split an Array A into Subsets having equal Sum and sizes equal to elements of Array B
- Divide binary array into three equal parts with same value
- Divide an array into k segments to maximize maximum of segment minimums
- Split the array into odd number of segments of odd lengths
- Equal sum array partition excluding a given element
- Print equal sum sets of array (Partition problem) | Set 1
- Print equal sum sets of array (Partition Problem) | Set 2
- Find three element from different three arrays such that a + b + c = sum
- Find three element from given three arrays such that their sum is X | Set 2
- Count ways to split a Binary String into three substrings having equal count of zeros
- Optimal partition of an array into four parts
- Partition the array into two odd length groups with minimized absolute difference between their median
- Count of ways to split an Array into three contiguous Subarrays having increasing Sum
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.