Why this code is not printing the correct output ? Can you please make it correct?

void cyclic(int a[] , int n)
{
int x , i, k;
int arr[n];
int Q;
cin>>Q;
for(k=1;k<=Q;k++)
{
cin>>x;
for( i=0;i<n;i++)
{
for(int j=i;j<i+1;j++)
{
arr[j] = a[i]+a[i-x];
//cout<<arr[j]<<" “;
}
}
for(int l=0;l<n;l++)
{
a[l] = arr[l];
//cout<<a[l]<<” ";
}
}
int temp =0 ;
for(i=0;i<n;i++)
{
temp = temp+a[i];
}
int ans=temp;
cout<<ans<<endl;

}
int main()
{
int n;
cin>>n;
int a[n];
for(int i=0;i<n;i++)
{
cin>>a[i];
}
cyclic(a,n);
return 0;
}

hi @deepshreyamishra301_1918130c8477386b,
LOGIC :
u dont need to calculate the sum by shifting again and again, anyway u will be adding the array sum of that array only even if u shift and add as u r adding all the elements so dont shift just keep updating the sum of array

see here for implementation https://ide.codingblocks.com/s/657171

In line22 u take x as input but cannot use it anywhere ???

@deepshreyamishra301_1918130c8477386b, I’ve used it to add to the sum

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.

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.