BIGARRAY: OBTAIN THE SUM

Problem:

Chef is given two integers N,SN,S.

Consider an array AA of size NN such that A[i]=iA[i]=i for every 1≤i≤N1≤i≤N.

You are required to find any position x(1≤x≤N)x(1≤x≤N) such that :

(A[1]+A[2]+…A[x−1])+(A[x+1]+A[x+2]+…A[N])=S(A[1]+A[2]+…A[x−1])+(A[x+1]+A[x+2]+…A[N])=S

If there are multiple such positions xx, print any such position. If there is no such position, print −1−1.

Solution:

#include <iostream>
using namespace std;
int main()
{
	int t;
	cin>>t;
	for(int i=0;i<t;i++)
	{
	    long long n,s;
	    cin>>n>>s;
	    long long x=(n*(n+1))/2-s;
	    if(x>=1 and x<=n)
	    cout<<x<<endl;
	    else
	    cout<<"-1"<<endl;
	}
	return 0;
}
See also  HNUM: Joy giving number