Skip to content

Commit b36f81a

Browse files
committed
Upate Notes
1 parent a1e343c commit b36f81a

24 files changed

Lines changed: 2738 additions & 771 deletions

pages/Abstract Classes.md

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -14,24 +14,27 @@ keywords: "abstract classes, OOP, pure virtual, C++, abstract base class, inheri
1414
collapsed:: true
1515
- ```mermaid
1616
classDiagram
17-
class Shape {
18-
<<abstract>>
19-
+area()* double
20-
+draw()* void
21-
}
22-
class Circle {
23-
-radius: double
24-
+area() double
25-
+draw() void
26-
}
27-
class Rectangle {
28-
-width: double
29-
-height: double
30-
+area() double
31-
+draw() void
32-
}
33-
Shape <|-- Circle
34-
Shape <|-- Rectangle
17+
class Shape {
18+
<<abstract>>
19+
+area() double
20+
+draw() void
21+
}
22+
23+
class Circle {
24+
-radius: double
25+
+area() double
26+
+draw() void
27+
}
28+
29+
class Rectangle {
30+
-width: double
31+
-height: double
32+
+area() double
33+
+draw() void
34+
}
35+
36+
Shape <|-- Circle
37+
Shape <|-- Rectangle
3538
```
3639
-
3740
- ## Advantages

pages/Bitwise Hacks.md

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
---
2+
seoTitle: Advanced Bitwise Hacks – O(1) Fast Formulas
3+
description: "Master advanced bitwise operations including Brian Kernighan's Algorithm, XOR Swaps, and Power of 2 checks."
4+
keywords: "bitwise hacks, brian kernighan algorithm, xor swap, power of 2, bit manipulation, fast formulas, O(1), VR-Rathod, Code-Note, code note vr, vr book"
5+
---
6+
7+
> [!info] What are Bitwise Hacks?
8+
> Bitwise hacks are extremely fast, low-level formulas that manipulate integer data directly at the binary level using bitwise operators (`&`, `|`, `^`, `~`, `<<`, `>>`).
9+
> They execute in a single CPU cycle ($O(1)$) and are essential for competitive programming, graphics engines, and performance-critical systems.
10+
11+
- # 1. Check if a Number is a Power of 2
12+
- **Formula**: `(x & (x - 1)) == 0`
13+
- **Explanation**: A power of 2 has exactly one bit set to `1` (e.g., `8` is `1000`). Subtracting `1` from it flips all bits starting from that single `1` downwards (e.g., `7` is `0111`). Therefore, `x & (x - 1)` clears that single bit, leaving `0`.
14+
- **Note**: Ensure `x > 0` before checking.
15+
- ```c++
16+
bool isPowerOfTwo(int x) {
17+
return x > 0 && (x & (x - 1)) == 0;
18+
}
19+
```
20+
21+
- # 2. Count Set Bits (Brian Kernighan’s Algorithm)
22+
- **Formula**: `x = x & (x - 1)`
23+
- **Explanation**: Instead of checking every bit in an integer ($O(\text{Bits})$ time), Brian Kernighan's algorithm drops the lowest set bit in each iteration.
24+
- **Time Complexity**: $O(K)$ where $K$ is the number of set bits (`1`s).
25+
- ```python
26+
def count_set_bits(x):
27+
count = 0
28+
while x > 0:
29+
x = x & (x - 1) # Clear the lowest set bit
30+
count += 1
31+
return count
32+
```
33+
34+
- # 3. Isolate the Rightmost 1-Bit
35+
- **Formula**: `x & (-x)`
36+
- **Explanation**: In Two's Complement representation, `-x` is equivalent to `(~x) + 1`. This mathematical property flips all bits above the lowest set bit, leaving only the rightmost set bit in common with `x`.
37+
- Highly useful in building the Fenwick Tree (Binary Indexed Tree).
38+
- ```c++
39+
int isolateRightmostBit(int x) {
40+
return x & (-x);
41+
}
42+
```
43+
44+
- # 4. The XOR Swap
45+
- **Formula**: `a ^= b; b ^= a; a ^= b;`
46+
- **Explanation**: Swaps the values of two variables without using a temporary third variable, utilizing the property $A \oplus B \oplus B = A$.
47+
- ```c++
48+
void swap(int &a, int &b) {
49+
if (&a != &b) { // Must not point to the same memory location!
50+
a ^= b;
51+
b ^= a;
52+
a ^= b;
53+
}
54+
}
55+
```
56+
57+
- # Key Takeaways
58+
collapsed:: true
59+
- Bitwise hacks exploit the binary representation of integers to replace loops and conditionals with single-instruction math.
60+
- They are the foundation of many high-performance data structures like Fenwick Trees and Zobrist Hashing.
61+
62+
- # More Learn
63+
collapsed:: true
64+
- ## GitHub & Webs
65+
- [Bit Twiddling Hacks (Stanford)](https://graphics.stanford.edu/~seander/bithacks.html)
66+
- [GeeksforGeeks -> Bit Magic](https://www.geeksforgeeks.org/bitwise-hacks-for-competitive-programming/)

pages/Bloom Filter.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,7 @@ keywords: "bloom filter, probabilistic data structure, set membership, false pos
238238
- # Variations & Related Concepts
239239
collapsed:: true
240240
- **Counting Bloom Filter**: Replaces each bit in the array with a small counter. Insertion increments the counter, and deletion decrements it, allowing dynamic item removal.
241-
- **Cuckoo Filter**: A newer alternative using cuckoo hashing tables to store fingerprints. It supports deletions and achieves better space efficiency for low false positive rates.
241+
- **Cuckoo Filter**: A newer alternative using [[cuckoo hashing]] tables to store fingerprints. It supports deletions and achieves better space efficiency for low false positive rates.
242242
- **Quotient Filter**: A cache-friendly alternative that stores fingerprints in a hash table using quotienting, supporting resizing.
243243
-
244244
- # Key Takeaways

pages/Bucket Sort.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -700,5 +700,5 @@ keywords: "bucket sort, bucketsort, distribution sort, sorting algorithm, unifor
700700
- # More Learn
701701
collapsed:: true
702702
- ## GitHub & Webs
703-
- [GeeksforGeeks -> Bucket Sort](https://www.geeksforgeeks.org/bucket-sort-2/)
704-
- [Wikipedia -> Bucket sort](https://en.wikipedia.org/wiki/Bucket_sort)
703+
- [Wikipedia -> Bucket sort](https://en.wikipedia.org/wiki/Bucket_sort)
704+
- [GeeksforGeeks -> Bucket Sort](https://www.geeksforgeeks.org/bucket-sort-2/)

pages/Cartesian Tree.md

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -385,4 +385,11 @@ keywords: "Cartesian Tree, Treap, Heap Property, BST Property, Monotonic Stack,
385385
- **Hybrid Structure** — acts as a BST for array indices and a Heap for values.
386386
- **Linear Time Construction** — can be built in $O(N)$ time using a monotonic stack.
387387
- **RMQ to LCA** — efficiently maps Range Minimum Queries to LCA queries in $O(1)$ time after preprocessing.
388-
- **Unique Trees** — given an array of distinct elements, the Cartesian Tree is always unique.
388+
- **Unique Trees** — given an array of distinct elements, the Cartesian Tree is always unique.
389+
-
390+
- # More Learn
391+
collapsed:: true
392+
- ## GitHub & Webs
393+
- [GeeksforGeeks -> Cartesian Tree](https://www.geeksforgeeks.org/cartesian-tree/)
394+
- [Wikipedia -> Cartesian tree](https://en.wikipedia.org/wiki/Cartesian_tree)
395+
- [CP-Algorithms -> Cartesian Tree](https://cp-algorithms.com/graph/treap.html)

0 commit comments

Comments
 (0)