Series of even

Practice

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

DIFFICULTY:

CAKEWALK, SIMPLE, EASY.

Math .

PROBLEM:

Series of even natural number is given . 2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 32 34 36 …… 10^9

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

for example :

N=5, Sum=24

you have to find missing term in first 5 number

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

EXPLANATION:

You simply given N and sum of first N even 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
N=5 sum=25
Sum of first N even number is 30 but sum is given 25 so if we subtract 25 with 30 we will get our answer that is 5
formula for sum of first N even natural number is (N*(N+1))

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);
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);
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);
long long int pp=k-sum;
cout<<pp;

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