Snippets Collections
//general approach Template
void Backtrack(int start)
{
    //Base case 

// loop for all possible values
{
    //include the current element at this position if possible in the ans 
	//More generally, make a choice 

    Backtrack(start+1) 

    //backtrack by removing current element 
}
                            ------------------------------------------------

//Generate all subsets (non dublicated)
int n;
void GenerateSubs(vector<int> &a, int s, vector<int> &subset, vector<vector<int>> &ans) {
    for (int i = s; i < n; i++) {
        subset.push_back(a[i]); //include element at ith position
        GenerateSubs(a, i + 1, subset, ans); //generate all subsets including ith element
        subset.pop_back(); //backtrack
    }
    ans.push_back(subset);
}

vector<vector<int>> subsets(vector<int> &a) {
    vector<vector<int>> ans;
    vector<int> subset;
    n = a.size();

    GenerateSubs(a, 0, subset, ans);
    return ans;
}

int main() {
    fast;
    int n;
    cin >> n;
    vector<int> a(n);
    vector<vector<int>> ans(n);
    for (int i = 0; i < n; ++i) {
        cin >> a[i];
    }
    ans = subsets(a);
    for (vector<int> i: ans) {
        for (int j: i) {
            cout << j;
        }
        cout << '\n';
    }
}
// i/p :
  //3
  //1,2,3
// o/p : 
                              ------------------------------------------------

//Generate all subsets (dublicated)
int n;
void GenerateSubs(vector<int>&a,int s,vector<int>&subset,vector<vector<int>>&ans)
{
    for(int i=s;i<n;i++)
    {
        if(i==s||a[i]!=a[i-1])
        {
            subset.push_back(a[i]); //include element at ith position
            GenerateSubs(a,i+1,subset,ans); //generate all subsets including ith element
            subset.pop_back(); //backtrack
        }
    }
    ans.push_back(subset);
}
vector<vector<int>> subsetsWithDup(vector<int> &a) {
    vector<vector<int>> ans;
    vector<int> subset;
    n = a.size();

    sort(a.begin(), a.end()); //sort the elements so that we can keep track of duplicates
    GenerateSubs(a, 0, subset, ans);
    return ans;
}

int main() {
    fast;
#ifndef ONLINE_JUDGE
    freopen("Input.txt", "r", stdin);
    freopen("Output.txt", "w", stdout);
    freopen("Error.txt", "w", stderr);
#endif
    int n;
    cin >> n;
    vector<int> a(n);
    vector<vector<int>> ans(n);
    for (int i = 0; i < n; ++i) {
        cin >> a[i];
    }
    ans = subsetsWithDup(a);
    for (vector<int> i: ans) {
        for (int j: i) {
            cout << j;
        }
        cout << '\n';
    }
}
// i/p :
  //4
  //1 2 3 2
/* o/p :
1223
122
123
12
13
1
223
22
23
2
3
  */
                              ------------------------------------------------
                              ------------------------------------------------

//---------------------------------------Examples--------------------------------------------------
//Generate all subset instead '?'
string s;
string o;

void solve(int i) {
    //base case
    if (i == s.length()) {
        for (int j = 0; j < s.length(); ++j) {
            cout << o[j];
        }
        cout << '\n';
        return;
    }
    if (s[i] != '?') {
        o[i] = s[i];
        solve(i + 1);
    } else {
        for (int j = 'a'; j <= 'z'; ++j) {
            o[i] = j;
            solve(i + 1);
        }
    }
}


int main() {
    cin >> s;
    solve(0);
}
---------------------------------------------------------------------------------------------------
//Generating Permutations Using next_permutation

int n;
int const N = 11;
bool taken[N];
int stk[N];

void prm(int i) {
    //base case
    if (i == n) {
        for (int j = 0; j < n; ++j) {
            cout << stk[j] << ' ';
        }
        cout << el;
        return;
    }
    for (int j = 1; j <= n; ++j) {
        if (!taken[j]) {
            taken[j] = true;
            stk[i] = j;
            prm(i + 1);
            taken[j] = false;
        }
    }
}


int main() {

    fast;
#ifndef ONLINE_JUDGE
    freopen("Input.txt", "r", stdin);
    freopen("Output.txt", "w", stdout);
    freopen("Error.txt", "w", stderr);
#endif
    cin >> n;
    prm(0);
}
---------------------------------------------------------------------------------------------------
//  
// Left shift ---> Multiplying by 2 
int x = 6;
x <<= 1;
cout << x;   // 12
--------------------------------------------------------------------------------------------------
//Right shift ---> Divide by 2 
int x = 12;
x >>= 1;
cout << x;   // 6
--------------------------------------------------------------------------------------------------

--------------------------------------------------------------------------------------------------
//  Checking if given 32 bit integer is power of 2 
// if true return 1 else return 0
int isPowerof2(int x)
{
    return (x && !(x & x-1));
}
--------------------------------------------------------------------------------------------------
// Find log base 2 of 32 bit integer
// log2(8)=3 when 2^3 = 8
int log2(int x)
{
    int res = 0;
    while (x >>= 1)
        res++;
    return res;
}
--------------------------------------------------------------------------------------------------
//1- (string) ---> (int)
stoi("String name");
 
//2- (int) ---> (string)
int n;
cin>>n;
String s = to_string(n);
cout<<s;
 
//3- (char) ---> (int)
String str;
cin>>str;
for(int i; str[i], i++){
  char ch = str[i]-'0';   //note:(str[i]-'0') to convert str[i] character to integer
  cout<<ch;
}
  
//3- (int) ---> (char)
int n;
cin>>n;
char ch = (char) n;
cout<<ch;
 
//4- (int) ---> (long long)
int a;
a = a*1LL;
 
//5- (double) ---> (int) && flooring number
double n = 7.99999999;
n = (int) n;
star

Thu Aug 18 2022 08:08:24 GMT+0000 (Coordinated Universal Time)

#c++ #data_types
star

Thu Aug 18 2022 08:07:34 GMT+0000 (Coordinated Universal Time)

#c++ #data_types
star

Thu Aug 18 2022 07:58:56 GMT+0000 (Coordinated Universal Time) https://www.geeksforgeeks.org/bit-tricks-competitive-programming/

#c++ #data_types
star

Thu Aug 18 2022 07:27:48 GMT+0000 (Coordinated Universal Time)

#c++ #data_types
star

Wed Aug 17 2022 20:45:01 GMT+0000 (Coordinated Universal Time) https://cses.fi/problemset/task/1085

#c++ #data_types
star

Wed Aug 17 2022 20:40:17 GMT+0000 (Coordinated Universal Time)

#c++ #data_types

Save snippets that work with our extensions

Available in the Chrome Web Store Get Firefox Add-on Get VS Code extension