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 :)  

Tom and jerry solution

JUNE LONG CHALLENGE:                   Problem code: EOEO The Tom and Jerry Game!  logic : For odd case: you just have to find all the values which are even. For even case : Jerry only wins when Jerry strength is even and tom strength is odd so we have to find only those values of JS which can divided by 2 more times than times who in simpler terms (or in bit terms ) we have to to find all that values which have mores zeroes in the end than TS CODE:  #include<bits/stdc++.h> using namespace std; int main(){     long long t;     cin>>t;     while(t--){         long long tom,jerry,count=0;         cin>>tom;         if(tom%2!=0)cout<<(tom-1)/2<<"\n"; /* if tom strength is odd  */     ...