Series of natural number

Practice

Author: Aryan KD
Tester: Aryan KD
Editorialist: Aryan KD

DIFFICULTY:

CAKEWALK, SIMPLE, EASY.

PREREQUISITES:

Math .

PROBLEM:

Series of natural number is given .

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 …… 10^9

you have to find a missing number if sum of first N number is given

for example :

N=5, Sum=10

you have to find missing term in first 5 number

according to above series sum of first 5 number is 15, and if we subtract 5 from it then sum becomes 10 hence the missing number is 5 .

EXPLANATION:

You simply given N and sum of first N number in that one term is missing so if we subtract the sum given in question with original sum that given we will get our answer.
for example :innocent:
N=5 sum=10
Sum of first N number is 15 but sum is given 10 so if we subtract 10 with 15 we will get our answer that is 5
formula for sum of first N natural number is (N*(N+1))/2

SOLUTIONS:

Setter's Solution

#include<bits/stdc++.h>
using namespace std;
void testcase()
{
long long int n,sum;
cin>>n>>sum;
long long int k=(n*(n+1))/2;
long long int pp=k-sum;
cout<<pp;

}
int main()
{
int t;
cin>>t;
while(t–)
{
testcase();
cout<<endl;
}
return 0;
}

Tester's Solution

#include<bits/stdc++.h>
using namespace std;
void testcase()
{
long long int n,sum;
cin>>n>>sum;
long long int k=(n*(n+1))/2;
long long int pp=k-sum;
cout<<pp;

}
int main()
{
int t;
cin>>t;
while(t–)
{
testcase();
cout<<endl;
}
return 0;
}

Editorialist's Solution

#include<bits/stdc++.h>
using namespace std;
void testcase()
{
long long int n,sum;
cin>>n>>sum;
long long int k=(n*(n+1))/2;
long long int pp=k-sum;
cout<<pp;

}
int main()
{
int t;
cin>>t;
while(t–)
{
testcase();
cout<<endl;
}
return 0;
}

I think there is a formatting problem with the test data that upsets Python. I got round this by handling the input values in a generator to deliver a uniform stream of integers:

def getvals():
    while True:
        yield from map(int, input().split())

T = int(input())
inp = getvals()
for tx in range(T):
    N = next(inp)
    S = next(inp)

followed by the actual problem solving part.