Given an array of integers, find two indices that split the array into three contiguous parts with equal sums. Return the indices or -1 if it is impossible.
๐ก Model Answer
First compute the total sum of the array. If it is not divisible by 3, return -1. Let target = total / 3. Iterate through the array while maintaining a running sum. When the running sum equals target for the first time, record that index as the end of the first part. Continue iterating; when the running sum equals 2 * target, record that index as the end of the second part. Ensure that the second index is not the last element, because the third part must contain at least one element. If both indices are found, return them; otherwise return -1. This algorithm runs in O(n) time and O(1) extra space.
This answer was generated by AI for study purposes. Use it as a starting point โ personalize it with your own experience.
๐ค Get questions like this answered in real-time
Assisting AI listens to your interview, captures questions live, and gives you instant AI-powered answers on a discreet on-screen overlay.
Get Assisting AI โ Starts at โน500