Need help with this error


#1

I am getting an error at a very particular value. The rest are correct. Pls go through the code (have used divide and conquer algorithm to determine the number of occurences of a the max value in sub array).
code:-

void occurence (vector &A, int l, int r, vector &G){
auto index = std::max_element(A.begin()+l, A.begin()+r+1) - A.begin();
auto occur = (r-index+1)*(index-l+1);
G.insert(G.end(), occur, A[index]);
int start1 = l;
int end1 = index-1;
int start2 = index+1;
int end2 = r;
if (start1<=end1){
occurence(A, start1, end1, G);
}
if (start2<=end2){
occurence(A, start2, end2, G);
}
}

vector Solution::solve(vector &A, vector &B) {
int i, end;
end = A.size()-1;
auto maxi = max_element(A.begin(),A.end());
vector G;
G.push_back(maxi);
occurence(A, 0, end, G);
float cnt=0;
int check;
int M = 1000000007;
for(i=0; i<G.size(); i++){
cnt = 0;
if (G[i] == check && i>0){
G[i] = G[i-1];
continue;
}
long long res = 1;
for(int j=1; j
j <= G[i]; j++){
if ( G[i]%j == 0){
if (G[i]/j == j){
res = (res * j) % M;
}
else{
res = (res * j) % M;;
res = (res * G[i]/j) % M;
}
}
}
check = G[i];
G[i] = res;
}

sort(G.begin(), G.end(), greater());

vector<int> X;

for(i=0; i<B.size(); i++){
    X.push_back(G[B[i]-1]);
}

return X;

}