# How could I got “array must be initialized with a brace-enclosed initializer”

• The code is as follows, if I move left[0]=A[0] and right[n-1]=A[n-1] outside of the for statement at line 8, the code will be accepted. Just curious about what happened and why get such error?

``````class Solution {
public:
int trap(int A[], int n) {
if(n == 0) return 0;
vector<int> left(n,0);
vector<int> right(n,0);
int leftMax = A[0], rightMax = A[n-1];
for(int i=1, left[0]=A[0], right[n-1]=A[n-1]; i<n; i++){
if(A[i]<=leftMax) left[i] = leftMax;
else{
left[i] = A[i];
leftMax = A[i];
}
if(A[n-1-i]<=rightMax) right[n-1-i] = rightMax;
else{
right[n-1-i] = A[n-1-i];
rightMax = A[n-1-i];
}
}

int volume = 0;
for(int i=0; i<n; i++){
int temp = min(left[i], right[i]) - A[i];
volume += temp;
}
return volume;
}
};``````

• Change

``````for(int i=1, left[0]=A[0], right[n-1]=A[n-1]; i<n; i++){
// It means create 3 new variables i, left[0] and right[n-1]. It doesn't make any sense.
``````

to

``````       left[0]=A[0];
right[n-1]=A[n-1];
for(int i=1 ; i<n; i++){``````

• You are right, thank you!

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.