CODING FOR FUN -EDITORIAL

Problem link:CodeChef: Practical coding for everyone
Setter:Ankur Pandey
Tester:Ankur Pandey

Difficulty:
Simple
Time Complexity:O(1)
Problem:…
Explanation:Sum of n terms of series if n is even is given by n(n+1)^2/2 and if n is odd then sum of n term of series is n^2(n+1)/2.
Next step is if sum is odd then print “Jhon” else print “Ravi”.
#include<bits/stdc++.h>

using namespace std;

int main()

{

int n,sum;

cin>>n;

if (n%2==0)

    sum=(n*pow((n+1),2))/2;

else

    sum=(pow(n,2)*(n+1))/2;

if (sum%2==0)

    cout<<"Ravi"<<endl;

else

    cout<<"Jhon"<<endl;

    

return 0;

}

Easiest Solution as per me …

#include <iostream>
using namespace std;

int main() {
    int n;
    cin>>n;
    if(n%4==0 || n%4==3)
        cout<<"Ravi"<<endl;
    else
        cout<<"Jhon"<<endl;
	return 0;
}

What do you think ? @ankur

1 Like