Calculate The Sum(array question)

2 Test cases passed, 1 failed

#include
#include
using namespace std;
void fun(int n, int arr[]){
int o;
cin>>o;
if(o>=0){

int arra[n];
for(int j=0;j<n;j++){
	arra[j]=arr[j];
}
int x;

int sum=0;
//Below loop takes the value of x multiple times
for(int j=1;j<=o;j++){
	cin>>x;
	if(x>=0 && x<n){
	//below loop calculates sum
for(int i=0;i<n;i++){
	if((i-x)>=0){
arr[i]=arr[i]+arra[i-x];
}
else if((i-x)<0){
	arr[i]=arr[i]+arra[n-x];
}
}
//below loop copies value of one array to other for  calculation in next iteration
	for(int j=0;j<n;j++){
	arra[j]=arr[j];
}
}
}

for(int i=0;i<n;i++){
	sum=sum+arr[i];
}
int a=(pow(10,9)+7);
sum=sum%a;

cout<<sum;
}

}

int main() {
int n;
cin>>n;
int arr[n];

if(n>=1 && n<=100000){
for(int x=0;x<n;x++){
	cin>>arr[x];
}
fun(n,arr);
}
return 0;

}

Hi @shuja33,

Correct condition should be,

   if((i-x)>=0){
          arr[i]=arr[i]+arra[i-x];
   }else{
          arr[i]=arr[i]+arra[n-x+i];
   }

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.