#PS0000117. Tìm vị trí cân bằng (Find Pivot Index)
Bài tập thuộc lộ trình
Mục lục đề ⌄
Tìm vị trí cân bằng (Find Pivot Index)
Find Pivot Index
Source: LeetCode
Version: Phuoc Hung OJ Extended
Problem Statement
An array of integers is indexed from 0. An index is balanced if the sum strictly to its left equals the sum strictly to its right. The element belongs to neither sum. An empty side has sum 0. Find the smallest balanced index, or return if none exists.
Input
The first line contains integer .
The second line contains integers .
Output
Print one integer: the smallest balanced index (zero-based), or if none exists.
Subtasks
- Subtask 1 (20 points): ; ; .
- Subtask 2 (30 points): ; ; .
- Subtask 3 (50 points): ; ; .
Example 1
Input
6
1 7 3 6 5 6
Output
3
Explanation
At indices 0, 1, and 2 the left/right sums are , , and . At index 3 they are and , so the smallest valid index is 3.
Example 2
Input
3
1 2 3
Output
-1
Explanation
The left/right pairs are , , and . None are equal.
Example 3
Input
3
2 1 -1
Output
0
Explanation
At index 0 the left side is empty and sums to 0; the right sum is . No smaller index is possible.