#include <bits/stdc++.h>
using namespace std;
int main()
{
ios::sync_with_stdio(false);
cin.tie(nullptr);
// Solution here
return 0;
}int t;
cin >> t;
while(t--) {
int n;
cin >> n;
// solve this test case
}for(int x : a) {
cout << x << " ";
}Range
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
// sort(all(v));
// sort(rall(v));
// reverse(all(v));for swapping
swap(a, b);swap header ->
#include <utility>Sorting header ->
#include <algorithm>// Sorting
sort(a.begin(), a.end());
sort(a, a + n);
// Descending:
sort(a.rbegin(), a.rend());
sort(a, a + n, greater<int>());
// min/max
min(a,b);
max(a,b);bool isEven(int x) {
return x % 2 == 0;
}
if(isEven(x)) {
cout << "Even";
}Standard Template
long long lo = 0, hi = 1e18;
while(lo < hi) {
long long mid = lo + (hi - lo) / 2;
if(check(mid))
hi = mid;
else
lo = mid + 1;
}Ascending order
sort(a.begin(), a.end());Descending order
sort(a.rbegin(), a.rend());for (string::iterator i = s.begin(); i < s.end(); i++)
cout << *i << " ";or,
for (auto i = s.begin(); i < s.end(); i++)
cout << *i << " ";For Frequency the occurance
1 2 2 3 3 3
// O(log n) access
map<int,int> mp;
for(int x : a) {
mp[x]++;
}mp[1] = 1
mp[2] = 2
mp[3] = 3
// O(1) access
unordered_map<int,int> mp;For storing unique values in sorted order
set<int> s;
s.insert(5);
s.insert(2);
s.insert(5);
s.insert(10);Output
2 5 10
Boolean array approach
bool seen[26] = {};
int distinct = 0;
for (char c : username)
{
int index = c - 'a';
if (!seen[index])
{
seen[index] = true;
distinct++;
}
}Set approach
set<char> uniqueCharacters;
for (char c : items)
uniqueCharacters.insert(c);- Character to Number (Index): SUBTRACT (
-)- Why? You want to "strip away" the base ASCII value to get a clean 0-based index or numerical value.
- Example:
s[i] - 'a'converts a letter character into an index from 0 to 25.
- Number (Index) to Character: ADD (
+)- Why? You want to "shift" a plain number up into the valid ASCII character range.
- Example:
i + 'a'converts an index 0–25 back into a letter character.
- Use
'0'when working with digits ('0'through'9'). - Use
'a'when working with lowercase letters ('a'through'z'). - Use
'A'when working with uppercase letters ('A'through'Z').
| Goal | Operation | Example | Explanation |
|---|---|---|---|
Char Digit to Int ('5' -> 5) |
c - '0' |
'5' - '0' |
53 - 48 = 5 |
Int to Char Digit (5 -> '5') |
num + '0' |
5 + '0' |
5 + 48 = 53 (prints '5') |
Char Letter to Index ('c' -> 2) |
c - 'a' |
'c' - 'a' |
99 - 97 = 2 |
Index to Char Letter (2 -> 'c') |
i + 'a' |
2 + 'a' |
2 + 97 = 99 (prints 'c') |
long long summation(long long arr[], int n)
{
if (n == 0) return 0;
return arr[n - 1] + summation(arr, n-1);
}