Skip to content

Instantly share code, notes, and snippets.

+==================+=================+==================+
| Solution | Time Complexity | Space Complexity |
+==================+=================+==================+
| Backtracking | O(2^n) | O(n) |
+------------------+-----------------+------------------+
| Bit Manipulation | (On*2^n) | O(1) |
+------------------+-----------------+------------------+
void all_subset(vector<int>& A, vector<int>& subset, int index)
{
print(subset);
for (int i = index; i < A.size(); i++)
{
subset.push_back(A[i]);
all_subset(A, subset, i + 1);
subset.pop_back();
}
return;
void all_subset(vector<int> a) 
{
int n=a.size(), c=0, i;
int total = pow(2, n); 
for(c= 0; c< total; c++) 
for(i= 0; i< n; i++) 
{
if(c&(1<< i)) 
void permutation(string str, int i)
{
int n=str.size()
if (i == n - 1)
{
cout << str << endl;
return;
}
for (int j = i; j < n; j++)
{
void permutation(string S, int n, string res)
{
if (n == 1)
{
cout << res + S << endl;
return;
}
for (int i = 0; i < n; i++)
{
| Approaches | Time Complexity | Space Complexity |
+==================+=================+==================+
| Reversing the lists and merging | O(n) | O(n) |
+------------------+-----------------+------------------+
| Merging the list and reversing | O(n) | O(n) |
+------------------+-----------------+------------------+
| Iterative Approach | (On) | O(1) |
+------------------+-----------------+------------------+
| Approaches | Time Complexity | Space Complexity |
+==================+=================+==================+
| Depth First Search Approach | O(4^n) | O(n) |
+------------------+-----------------+------------------+
| Breadth-First Search Approach | O(!n) | O(n) |
+------------------+-----------------+------------------+
| Approaches | Time Complexity | Space Complexity |
+==================+=================+========================+
| Brute Force Approach | O(n^3) | O(1) |
+------------------+--------------------+---------------------+
| Sliding Window Approach| O(n^2) | O(1) |
+------------------+--------------------+---------------------+
| Hash-Table Approach | (On) | O(1) |
+------------------+-----------------+------------------------+
| Approaches | Time Complexity |Space Complexity|
+==================+=================+==================+
| Recursive Method | O(n) | O(n) |
+------------------+-----------------+------------------+
| Iterative Method | O(n) | O(n) |
+------------------+-----------------+------------------+
| Approaches | Time Complexity |Space Complexity|
+==================+=================+==================+
| Level Order Traversal| O(n) | O(n) |
+------------------+-----------------+------------------+
| Recursive Function | O(n) | O(n) |
+------------------+-----------------+------------------+