给定&的零索引数组该数组的平衡指数 [英] A zero-indexed array given & An equilibrium index of this array
问题描述
给出了一个由N个整数组成的零索引数组A.该阵列的平衡指数是任何整数P,使得0≤P<0. N和较低索引元素的总和等于较高索引元素的总和,即 A [0] + A [1] + ... + A [P-1] = A [P + 1] + ... + A [N-2] + A [N-1]. 假定零元素之和等于0.如果P = 0或P = N-1,则可能发生这种情况.
例如,考虑由N = 8个元素组成的以下数组A:
A[0] = -1
A[1] = 3
A[2] = -4
A[3] = 5
A[4] = 1
A[5] = -6
A[6] = 2
A[7] = 1
P = 1是该数组的平衡指数,因为:
A[0] = −1 = A[2] + A[3] + A[4] + A[5] + A[6] + A[7]
P = 3是该数组的平衡指数,因为:
A[0] + A[1] + A[2] = −2 = A[4] + A[5] + A[6] + A[7]
P = 7也是一个平衡指数,因为:
A[0] + A[1] + A[2] + A[3] + A[4] + A[5] + A[6] = 0
并且没有索引大于7的元素.
P = 8不是平衡指数,因为它不满足条件0≤P<1. N.现在我必须编写一个函数:
int solution(int A[], int N);
,给定一个由N个整数组成的零索引数组A,它返回其任何平衡索引.如果不存在平衡指数,则该函数应返回-1.
例如,给定上面显示的数组A,该函数可以返回1、3或7,如上所述.
假设:
N is an integer within the range [0..100,000];
each element of array A is an integer within the range [−2,147,483,648..2,147,483,647].
这里有些复杂:
Elements of input arrays can be modified.
JavaScript得分100
function solution(V) {
var sum = 0;
for (i=0; i < V.length; i++) {
sum += V[i];
}
var leftSum= 0;
var rightSum = 0;
for (j=0; j < V.length; j++) {
rightSum = sum - (leftSum + V[j]);
if(leftSum == rightSum) {
return j;
}
leftSum += V[j];
}
return -1;
}
A zero-indexed array A consisting of N integers is given. An equilibrium index of this array is any integer P such that 0 ≤ P < N and the sum of elements of lower indices is equal to the sum of elements of higher indices, i.e. A[0] + A[1] + ... + A[P−1] = A[P+1] + ... + A[N−2] + A[N−1]. Sum of zero elements is assumed to be equal to 0. This can happen if P = 0 or if P = N−1.
For example, consider the following array A consisting of N = 8 elements:
A[0] = -1
A[1] = 3
A[2] = -4
A[3] = 5
A[4] = 1
A[5] = -6
A[6] = 2
A[7] = 1
P = 1 is an equilibrium index of this array, because:
A[0] = −1 = A[2] + A[3] + A[4] + A[5] + A[6] + A[7]
P = 3 is an equilibrium index of this array, because:
A[0] + A[1] + A[2] = −2 = A[4] + A[5] + A[6] + A[7]
P = 7 is also an equilibrium index, because:
A[0] + A[1] + A[2] + A[3] + A[4] + A[5] + A[6] = 0
and there are no elements with indices greater than 7.
P = 8 is not an equilibrium index, because it does not fulfill the condition 0 ≤ P < N.
Now i have to write a function:
int solution(int A[], int N);
that, given a zero-indexed array A consisting of N integers, returns any of its equilibrium indices. The function should return −1 if no equilibrium index exists.
For example, given array A shown above, the function may return 1, 3 or 7, as explained above.
Assume that:
N is an integer within the range [0..100,000];
each element of array A is an integer within the range [−2,147,483,648..2,147,483,647].
here have some Complexity:
Elements of input arrays can be modified.
100 Score in Javascript
function solution(V) {
var sum = 0;
for (i=0; i < V.length; i++) {
sum += V[i];
}
var leftSum= 0;
var rightSum = 0;
for (j=0; j < V.length; j++) {
rightSum = sum - (leftSum + V[j]);
if(leftSum == rightSum) {
return j;
}
leftSum += V[j];
}
return -1;
}
这篇关于给定&的零索引数组该数组的平衡指数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!