Skip to content

devnickverma/CheatSheet-CPP

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 

Repository files navigation

CheatSheet-CPP

Quick CPP Codes

Resources

1. 10^9 use long long int

Basic template for 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

2. Sum of 1 to n even & odd numbers formula

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. COOL NAME CODECHEF

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

int main()
{
    int t;
    cin >> t;
    while(t--) {
     string s; cin>>s;
     sort(s.begin(), s.end());
     int sum=0;
     for(int i=0; i<s.size(); i++){
       int pos = int(s[i])-96;
       int ans = pos*(i+1);
       sum += ans;
      }
      cout<<sum<<endl;
    }
    return 0;
}

5. Non-Negative Product

#include<bits/stdc++.h>
#define ll long long
#define int ll
using namespace std;

int32_t main()
{
    int t,n,x;
    cin >> t ;
    while(t--) {
        cin >> n;
        bool flag = false;
        int count = 0;
        for(int i=0; i<n; i++) {
            cin >> x ;
            if(x<0) {
                count++;
            }
            if(x==0) {
                flag = true;
            }
        }
        if(flag) {
            cout << 0 << endl;
        }
        else if (count%2==0) {
            cout << 0 << endl;
        }
        else {
            cout << 1 << endl;
        }
    }
    return 0;
}

6. Two Different Palindromes

#include<iostream>
using namespace std;

int main()
{
    int t,x,y;
    cin >> t;
    while(t--) {
        cin >> x >> y;
        if( (x%2!=0) && (y%2!=0) ) {
            cout << "NO" << endl;
        }
        else if ( (x==1) || (y==1) ) {
            cout << "NO" << endl;
        }
        else {
            cout << "YES" << endl;
        }
    }
}

7. Even Splits

#include<bits/stdc++.h>
#define ll long long
#define int ll
using namespace std;

int32_t main()
{
    int t,n;
    cin >> t;
    while(t--) {
        cin >> n;
        string s;
        cin >> s;
        if((s[0]=='1') && (s[1]=='0') && s.size()==2) {
            cout << s << endl;
        }
        else {
            sort(s.begin(),s.end());
            cout << s << endl;
        }
    }
}

8. Stick length part

for _ in range(int(input())):
    n, k = map(int, input().split())
    print(1 if n%k > 0 else 0)


for _ in range(int(input())):
    n = int(input())
    if n <= 3:
        print(-1)
        continue
    print(*range(2, n+1, 2), *range(1, n+1, 2))

9.Small fact

#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;
}
10. 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;
}

11. string : I am smith => smith am I

#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;
}

// Python code
// s = "I am devnick verma"
// lst = s.split(' ')
// # lst ['I', 'am', 'devnick', 'verma']

// lst = lst[::-1]
// # lst ['verma', 'devnick', 'am', 'I']

// " ".join(lst)
// # 'verma devnick am I'

12. count every character frequence

#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;
}

// o/p count every character frequence