What is mistake in my code

solution link

for(int i=l-1;i<=r-1;i++){
ar[i]=((x+i+1-l)*(x+i+1-l))+ar[i];
// HERE IS THE ISSUE

since the array is 1-indexed , i.e. it starts from 1 to n … and similarly i has to toggle from L to R , instead of L-1 to R-1 …

try reducing the index of ar i.e. ar[i-1] and i should toggle from L to R (both included).

1 Like
ACfied Code
#include <bits/stdc++.h>
using namespace std;

int main() {
	// your code goes here
	 ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    
    unsigned long long int n,q;
	cin>>n>>q;
	long long int ar[n];
	for(int i = 0; i < n; i++) {
	    cin >> ar[i];
	}
	int t;
	while(q--)
	{
	    cin>>t;
	    if(t==1)
	    {
	         int l, r;
	         long long int x;
	         cin>>l>>r>>x;
	         for(int i=l-1;i<=r-1;i++){
	             ar[i]=((x + i + 1 - l) * (x + i + 1 - l)) + ar[i];
	         }
	    }
	    else
	    {
	        unsigned int h;
	        cin>>h;
	        cout << ar[h-1]<<"\n";
	    }
	    
	   
	}
	return 0;
}

Try finding out where it went wrong.

1 Like