Skip to main content

CSES PROBLEMSET

INTRODUCTORY PROBLEMS



1. WEIRD ALGORITHM

SOLUTION:

#include<bits/stdc++.h>
using namespace std ;
int main(){
long long n; //beacuse n can be greater than max size of int.
cin>>n;
while(n!=1){ // loop will break when n become 1.
cout << n << " ";
if(n&1)n = n *3+1; // when n is odd.
else n = n/2; // when n is even.
}
cout<< "1";
}



2. MISSING NUMBER

SOLUTION:

LOGIC: just sum all the inputs and subtract from total sum(n*(n+1)/2).

#include<bits/stdc++.h>
using namespace std;
int main(){
       long long n,s,sum=0;    
        cin>>n;
        for(long long i=0;i<n-1;i++){
           cin>>s;sum+=s; //sum all the inputs.
        }
        cout<<((n*(n+1))/2)-sum; // and subtracting from total sum.
}

Comments

Post a Comment

Popular posts from this blog

INTRO

Who am I?.   I am Manish Gupta,a Computer Science Engineering undergrad pursuing my B.Tech in NIT Srinagar and a competitive programmer.I am well versed in C++,Python,JavaScript,C and Bash and as programmer I am eager to learn new things.I'll make sure to post each and every line of elegant code that I write and also ensure to share solutions of some beautiful problems that I encounter.I'll also share some of the projects that I happen to work on. I am also interested in Ethical hacking and am looking forward to pursue a side career in it so I might share some of the "shady" code here :)  

The Delicious Cake

JUNE LONG CHALLENGE                                             Problem code : Contain /* Algorithm :-   --> We will create concentric hulls as many as possible.        This can be done using a tracking array to keep track of points which are still unused and passing.        it again in convex hull function We will do this until our track array becomes empty . Algorithm used :-        Finding convex hull using graham scan in O(nlogn) ( For AC )        Finding convex hull using jarvis march in O(n^2) ( For 35 points )  --> Then we will memoize all these convex hulls.        Now we cant create hulls for each query as it will result in  O(q*n^2) ...