๐
Date: Feb 13, 2026
๐ฅ Topic: The Grand Revision (C++, DSA, Logic & Pointers)
๐ The 42-Day Cheat Sheet
I started this journey on Jan 1st. Today, I am pausing to look back. This is not just a summary; this is my Revision Notes for everything I have learned so far. From "Hello World" to "Kadane's Algorithm".
1️⃣ The Foundation: C++ Basics
| Concept | Key Takeaway |
|---|---|
| Boilerplate | #include <iostream> is mandatory. Execution starts at main(). |
| Data Types | int (4 bytes), char (1 byte), float (4 bytes), double (8 bytes), bool (1 bit). |
| Input/Output | cout << to Print. cin >> to Input. |
Control Flow (Logic Gates)
- If-Else: For decision making.
- For Loop: When iteration count is known (
i=0; i<n; i++). - While Loop: Entry control (Check first, then run).
- Do-While: Exit control (Run once, then check).
- Break: Stop loop immediately.
- Continue: Skip current iteration.
2️⃣ Functions & Pointers (Memory Management)
This was the turning point where I learned how memory works.
| Pass by Value | Pass by Reference (&) |
|---|---|
| Values are copied. | Original memory address is passed. |
| Changes inside function DON'T affect main. | Changes DO affect main. |
void fun(int x) |
void fun(int &x) |
Pointer Cheat Sheet
int a = 10;
int *ptr = &a; // Stores address of a
cout << ptr; // Prints Address (0x7f...)
cout << *ptr; // Dereference: Prints Value (10)
3️⃣ Arrays & Strings (Data Storage)
- Array: Fixed size, contiguous memory. Index starts at 0.
- 2D Array: Grid logic (Rows & Cols). Nested loops required.
- Strings: Array of characters ending with
\0(Null Terminator). - String Input:
cinstops at space. Usegetline(cin, s)for full sentences.
Important String Functions
s.length(), s.push_back(), s.pop_back(), s.substr(start, len)
4️⃣ Algorithms: Sorting & Searching
Time Complexity: O(1) is best. O(n²) is slow. O(log n) is for binary logic.
| Algorithm | Logic Name | Complexity |
|---|---|---|
| Linear Search | Check one by one | O(n) |
| Bubble Sort | Push heaviest to end (Swapping) | O(n²) |
| Selection Sort | Find Minimum, put at start | O(n²) |
| Insertion Sort | Card Game (Insert in correct spot) | O(n²) |
| Inbuilt Sort | sort(v.begin(), v.end()) |
O(n log n) |
5️⃣ Bit Manipulation (The Hacker Zone)
Dealing with 0s and 1s directly.
- AND (&): Both 1 → 1.
- OR (|): Any 1 → 1.
- XOR (^): Different → 1, Same → 0.
- Left Shift (<<): Multiply by 2.
- Right Shift (>>): Divide by 2.
Legendary Tricks
// 1. Check Odd/Even
if (n & 1) cout << "Odd";
// 2. Check Power of 2
if ((n & (n-1)) == 0) cout << "Yes";
// 3. Set ith Bit
n = n | (1 << i);
6️⃣ Modern C++: Vectors & Memory
The upgrade from Arrays.
| Stack Memory | Heap Memory |
|---|---|
| Small, Auto managed. | Large, Manual (`new`/`delete`). |
| Static Arrays. | Dynamic Arrays & Vectors. |
Vector Powers: Dynamic resizing, capacity() vs size(), easy insertion/deletion.
7️⃣ Critical Logic Patterns
A. Two Pointers
Used in Palindrome, Pair Sum, Reverse Array.
One pointer at start, one at end. Move them closer.
B. Kadane’s Algorithm (Max Subarray Sum)
The most important Array algo.
int curSum = 0, maxSum = INT_MIN;
for(int x : arr) {
curSum += x;
maxSum = max(maxSum, curSum);
if(curSum < 0) curSum = 0; // Reset logic
}
C. Subarray vs Subsequence
- Subarray: Contiguous (Slice of bread). O(n²) count.
- Subsequence: Order matters, continuity doesn't (Toppings). 2^n count.
๐ What's Next?
The foundation is solid. The next phase of SahilLog will cover:
1. Recursion (Solving problems by breaking them down).
2. Time & Space Complexity Analysis in depth.
3. Object Oriented Programming (OOPs).
The grind continues.
No comments:
Post a Comment