#C10451. Find the Balanced Index
Find the Balanced Index
Find the Balanced Index
You are given an array of integers. Your task is to find the index i such that the sum of the numbers to the left of i is equal to the sum of the numbers to the right of i. Formally, you need to find an index i (0-indexed) such that
$$\sum_{k=0}^{i-1} nums[k] = \sum_{k=i+1}^{n-1} nums[k] $$If there exists such an index, output the first one (with the smallest index). If no such index exists, output -1
.
Note: If the array is empty, you should output -1
. For an array with only one element, the balanced index is 0
since both sums are considered to be 0.
inputFormat
The first line of input contains an integer n indicating the number of elements in the array. If n is greater than 0, the second line contains n space-separated integers representing the elements of the array. If n is 0, there is no second line.
outputFormat
Output a single integer — the balanced index of the array based on the described condition. If no balanced index exists, output -1
.
5
1 2 3 4 6
3