Skip to main content

Posts

C++ Day 39

  C++ Day 39 STL Containers (Deep Understanding & Real Usage) Till now, you already know arrays, vectors, loops, and STL algorithms. Today, we go one step deeper and understand STL containers , which are the backbone of modern C++ programming. In real projects and competitive coding, choice of container matters a lot. 1. What are STL Containers? STL containers are data structures provided by C++ to store data efficiently. They handle: memory management resizing element access performance optimization You focus on logic , not memory handling. 2. Categories of STL Containers STL containers are mainly divided into: Sequence Containers Associative Containers Unordered Containers Container Adapters 3. Sequence Containers These store data in sequence . 3.1 Vector Most used container in C++. vector< int > v; Key Features: Dynamic size Contiguous memory Fast random access Slower insertion in middle Example: v. push_...
Recent posts

C++ Day 38

  C++ Day 38 STL Algorithms (Practical & Real-World Usage) Until now, you have learned about loops, arrays, vectors, functions, and STL containers. From today, you move into the real power of C++ , where you stop writing long loops again and again and instead use ready-made logic provided by the Standard Template Library (STL). These are called STL Algorithms . 1. What are STL Algorithms? STL algorithms are predefined functions that perform common operations like: searching sorting counting modifying data checking conditions They work on ranges , not directly on containers. A range is defined using: container. begin (), container. end () This design makes algorithms container-independent . 2. Why Use STL Algorithms Instead of Loops? Consider this loop: int sum = 0 ; for ( int i = 0 ; i < v. size (); i++) { sum += v[i]; } Now compare with: int sum = accumulate (v. begin (), v. end (), 0 ); Advantages: Less code Fewer bugs ...

C++ Day 37

  Here’s C++ Day 37 with lots of rich, structured content so your daily practice stays meaningful. This includes: ✔ Concepts ✔ Problems ✔ Code templates ✔ Mini-projects ✔ Debug tasks ✔ Interview-style challenges 🚀 C++ Day 37 – Advanced Mixed Practice (Deep Dive Edition) 1️⃣ Concept of the Day — “Bitmasking + DP + Greedy Hybrids” Many advanced contest problems combine bit operations , DP , and greedy pruning . 👉 Key Points Use bitmasks to represent subsets. Use greedy heuristics to prune DP states. Use __builtin_popcountll() for fast operations. Precompute masks for speed. ⭐ Example Pattern for ( int mask = 0 ; mask < ( 1 <<n); mask++) { int bits = __builtin_popcount(mask); dp[mask] = ...; } 2️⃣ Quick Practice Problems (with difficulty) Try to solve these after studying the concept: (A) Easy — "Count Bits in Ranges" Given a range [l, r] , count total number of 1-bits in all numbers. (B) Medium — "Smallest Missin...

CSES Increasing Subsequence solution

 You are given an array containing  n n n integers. Your task is to determine the longest increasing subsequence in the array, i.e., the longest subsequence where every element is larger than the previous one. A subsequence is a sequence that can be derived from the array by deleting some elements without changing the order of the remaining elements. Input The first line contains an integer n n n : the size of the array. After this there are n n n integers x 1 , x 2 , … , x n x_1,x_2,\ldots,x_n x 1 ​ , x 2 ​ , … , x n ​ : the contents of the array. Output Print the length of the longest increasing subsequence. Constraints 1 ≤ n ≤ 2 ⋅ 1 0 5 1 \le n \le 2 \cdot 10^5 1 ≤ n ≤ 2 ⋅ 1 0 5 1 ≤ x i ≤ 1 0 9 1 \le x_i \le 10^9 1 ≤ x i ​ ≤ 1 0 9 Example Input: 8 7 3 5 3 6 2 9 8 Output: 4 #include < bits / stdc ++. h > using namespace std ; void solve (){ int n ; cin >> n ; vector <int> arr ( n ); for ( int i = 0 ; i < n ; i ++)...

C++ Day 36

  Day 36: Binary Search on Answer (BSOA) A high-level competitive programming technique. 🚀 1. What is Binary Search on Answer? You use binary search on the value of the answer when: 1️⃣ The answer lies in a range (like 1 to 1e18) 2️⃣ For any candidate mid , you can check whether it's possible , valid , or feasible 3️⃣ If mid is valid → all values < mid or > mid are also valid (monotonic property) This works for problems like: minimum time minimum capacity maximum possible score partitioning array into k parts scheduling optimization 🚦 2. Template long long lo = L, hi = R, ans = R; while (lo <= hi) { long long mid = (lo + hi) / 2 ; if ( check (mid)) { ans = mid; hi = mid - 1 ; // try to minimize } else { lo = mid + 1 ; } } cout << ans << "\n" ; 🧠 3. Example 1: Allocate Minimum Pages Given n books, allocate to k students to minimize maximum pages. check(mid) r...

C++ Day 35

  C++ Day 34: Layout Layouts (Part 2) We’ll cover: Constructer Layout Adjuster Layout Decorator Layout practise Task 🔹 1. developer form (creational) used to make compound objects measure away step ✅ employ case: you need to form associate in nursing aim (like amp pizza pie calculator house) with elective parameters example: cpp copy edit class calculator {     train Methodor gpu ram; public:     family developer {         train Methodor gpu ram;     public:         developer setcpu(string c) { Methodor = c; take *this; }         developer setgpu(string g) { gpu = g; take *this; }         developer setram(string r) { run = r; take *this; }         calculator Construct() {             take Calculater(cpu gpu ram);         }     };     Calculater(string snow train m train r) : cpu(c) gp...