sumitgirwal / Cheat-Sheet

CPP Cheat Sheet

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Cheat-Sheet

CPP Cheat Sheet for CP

Resources

1. 10^9 long long

2. Sum of 1 to n even & odd numbers

Sum of even : (n*n)
Sum of odd : (n*(n+1))

3. Find sum of all pair in array

Using map : ans += (i.second*(i.second - 1))/2;

4.

5.Small fact

// We have populated the solutions for the 10 easiest problems for your support.
// Click on the SUBMIT button to make a submission to this problem.

#include <bits/stdc++.h>
#include <boost/multiprecision/cpp_int.hpp>
#include <iostream>
using namespace std;
using namespace boost::multiprecision;

int main() {
	// your code goes here
	int t;
	cin>>t;
	while(t--)
	{
	    int n;
	    cin>>n;
	    cpp_int fact=1;
	    for(int i=n;i>0;i--)
	     fact=fact*i;
	    cout<<fact<<endl; 
	}
	
	return 0;
}
6. Always use string to input digits
#include <iostream>
#include <string>
using namespace std;

int32_t main() {
    int t; cin >> t;
    while (t--) {
		int d; string n; 
		cin>>d; cin>>n;  
		bool flag=false;
		for(int i=0;i<d;i++){	
			if(n[i]=='5' ||n[i]=='0')
			{ flag=true;
				break;
			}
		}		
		if(flag) cout<<"YES"<<endl;
		else cout<<"NO"<<endl;	
    }
    return 0;
}
long long
#include <iostream>
#include <vector>
using namespace std;
#define ll long long
#define int ll
int32_t main() {
    int t; cin >> t;
    while (t--) {
			int n; cin>>n;
			vector<int> a(n);
			for(int i=0;i<n;i++) cin>>a[i];
			int count=0;
			int temp=0;
			for(int i=0;i<n;i++){
				int x; cin>>x;
				if(x<=(a[i]-temp)) count++;
				temp = a[i];
			}
			cout<<count<<endl;
    }
    return 0;
}

Input: string bin_string = "10101010";

Function call:
stoi(bin_string, 0, 2);

Output:
170

#include<bits/stdc++.h>
using namespace std;

int32_t main()
{
    // input
    string str;
    getline(cin,str);
    
    int t = str.size();
    map<char,int> mp;
    
    // processing
    for(int i=0; i<t; i++) {
        if(str[i]!=' ') {
            mp[str[i]]++;
        }
    }
    
    //output
    for(auto i:mp) {
        cout << i.first << " = " << i.second << " ";
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;

int main() {
	
	// input
    string str;
    getline(cin,str);
    
    int n = str.size();
    string final="";
    string temp="";
    
    // processing
    for(int i=0; i<n; i++){
        
        if(str[i]==' '){
            final = " "+temp+final;
			temp = "";
        }else{
            temp = temp+str[i];
        }
    }

    if(temp != " "){
        final = " "+temp+final;
    }

    // output    
    cout<<final<<endl;

	return 0;
}