Given an array of integers, find the index where the sum of the elements to the left is equal to the sum of the elements to the right.
Explanation: Step-by-step: Given array [1, 2, 3, 4, 5], we calculate the left sum (1) and right sum (7) at index 0. Since the left sum is equal to the right sum, the pivot index is 0.
Explanation: Step-by-step: Given array [1, 2, 3, 4, 5, 6], we calculate the left sum (1+2+3 = 6) and right sum (4+5+6 = 15) at index 2. Since the left sum is not equal to the right sum, we move to the next index. At index 3, the left sum (1+2+3+4 = 10) is not equal to the right sum (5+6 = 11). At index 4, the left sum (1+2+3+4+5 = 15) is equal to the right sum (6 = 6). Therefore, the pivot index is 4.
Analyze constraints and compute optimal solutions step-by-step.
No dry run loaded.
🚀 Practice this problem
Run code, get AI hints & track streak