🟢 ⚪ 🔴 DSA Practice Update: Dutch National Flag Algorithm Today I implemented the classic Dutch National Flag problem — sorting an array of 0s, 1s, and 2s in-place. Example input: [1,0,2,0,1,0,2,0,1,0,2,0,1] 🧠 The Strategy (Three Pointers) Instead of sorting traditionally: ✔ Use low, mid, high ✔ Partition array into regions ✔ Swap based on value at mid Rules: • 0 → swap with low, move low++, mid++ • 1 → just mid++ • 2 → swap with high, move high-- ⚡ Why This Algorithm is Beautiful ✅ One pass → O(n) ✅ In-place → O(1) space ✅ No extra arrays ✅ Clean pointer logic 🎯 Output Perfectly grouped: [0s | 1s | 2s] This problem is a great reminder: 👉 Smart pointer movement > brute-force sorting What DSA algorithm impressed you recently? 👇 #DSA #DutchNationalFlag #Algorithms #TwoPointers #CodingJourney #ProblemSolving
Dutch National Flag Algorithm: Sorting 0s, 1s, 2s in-place
More Relevant Posts
-
🚀 Day 11/50 DSA Solved: Count Binary Substrings Today I solved an interesting string problem that strengthened my understanding of pattern recognition and optimized counting techniques. 🔍 Problem Statement Given a binary string s, count the number of non-empty substrings that: Contain equal number of 0s and 1s Have all 0s and 1s grouped consecutively 📌 Example Input: "00110011" Output: 6 🧠 Brute Force. 1️⃣ Generate all possible substrings using two loops → O(n²) 2️⃣ For each substring: Count number of 0s and 1s Check if counts are equal 3️⃣ Ensure characters are grouped (only one transition allowed between 0 and 1) 4️⃣ If valid → increment answer 📊 Complexity ⏱ Time Complexity: O(n³) 📦 Space Complexity: O(1) 🟢 Optimized Approach 🔥 Key Observation: Valid substrings only exist between two consecutive groups. Example: "00110011" Groups: 00 → 2 11 → 2 00 → 2 11 → 2 Valid substrings between adjacent groups = min(group1, group2) So: min(2,2) + min(2,2) + min(2,2) = 2 + 2 + 2 = 6 ⏱ Optimized Complexity: Time: O(n) ✅ Space: O(1) ✅ #LeetCode #DSA #Algorithms #Cpp #ProblemSolving #CodingJourney
To view or add a comment, sign in
-
-
We’ve all struggled with long-context inference in LLMs — slow attention, huge KV-cache, and latency that kills interactivity. Is there a way to internalize context instantly instead of continually re-feeding it? In a new paper, researchers introduce Doc-to-LoRA (D2L), a lightweight hypernetwork that learns to convert a full context into a LoRA adapter in a single forward pass. Here’s what’s compelling about it: - Traditional context distillation can internalize information into parameters, but it’s slow and expensive. D2L meta-learns this process so that the hypernetwork can generate an adapter tailored to a context with minimal compute. - Once internalized, the model can answer queries without re-reading the original context, dramatically reducing inference latency and KV-cache memory use. - On long-document QA tasks and synthetic “needle-in-a-haystack” setups, D2L generalizes beyond the base LLM’s native context window, retaining performance with far less overhead. This work points toward a future where models can absorb context into weights on the fly, enabling faster interactive systems and more efficient long-form understanding. We’d love to hear your thoughts on whether context internalization will become a standard part of LLM workflows. Link in the comments. #InContextLearning #LLMResearch #AIInfrastructure #ModelEfficiency
To view or add a comment, sign in
-
-
Bigger beam. More test-time compute. Better performance? Not always! Excited to share our new preprint. In this work, we show that increasing compute at test time does not always improve reasoning performance. We provide both theoretical and empirical results to better understand a key tradeoff: the relationship between evaluator quality and beam-search width in reasoning. One takeaway: scaling search alone is not enough; the quality of the evaluator matters (a lot).
More test-time compute can actually hurt LLM reasoning ⚠️ Beam search is often treated as a free lunch: wider beam, more candidates, better answers. In our new paper, we show that after a certain point, the opposite can happen. The reason is overestimation bias: when beam search picks the max over many noisy scores, wrong reasoning paths can get an artificial boost 📈, so past a certain point, wider search can favor paths that score higher but are actually worse. We call it reward inversion ↕️ We formally derive a maximum useful beam width, determined by the scorer’s signal-to-noise ratio. This also suggests a practical recipe for adaptive test-time compute ⚙️ Start narrow, expand while the signal is strong, and early-stop when extra beam width mostly adds noise. The takeaway is simple: Don’t just scale test-time compute. Scale the quality of the signal guiding the search. 🎯 Enjoyed working on this with Assaf Hallak, GAL CHECHIK and Yftah Ziser. 📄 Paper: https://lnkd.in/d9EFdd_c 💻 Code coming soon
To view or add a comment, sign in
-
-
SoTA retrieval models are now natively supported in TopK through multi-vector retrieval. Our algorithm matches or exceeds quality of existing methods while providing an order of magnitude better query latencies and CRUD capabilities. Check out our blog post for more details: https://lnkd.in/dEHpZWjJ
To view or add a comment, sign in
-
It's been only three months since Martin Spišák joined TopK, and we already have a new SoTA method for multi-vector retrieval. More to come on our quest to deliver the highest-quality context for agents 🚀
SoTA retrieval models are now natively supported in TopK through multi-vector retrieval. Our algorithm matches or exceeds quality of existing methods while providing an order of magnitude better query latencies and CRUD capabilities. Check out our blog post for more details: https://lnkd.in/dEHpZWjJ
To view or add a comment, sign in
-
🤚 Today's Learning – Sliding Window Technique 👉 Sliding Window Technique, an optimization pattern used for problems involving subarrays or substrings. Instead of recalculating results for every subarray using nested loops O(n²), the sliding window reuses previous computations by expanding and shrinking a window across the array. Example (window size = 3) Array: [1 2 3 4 5] [1 2 3] [2 3 4] [3 4 5] Key idea: Add the new element entering the window Remove the element leaving the window This reduces complexity from: O(n²) ➝ O(n) 👉 Two Types of Sliding Window • Fixed window size (e.g., Maximum sum subarray of size k) • Variable window size (e.g., Subarray with given sum) 👉 Common Problems • Maximum sum subarray of size k • Subarray with given sum • Longest substring without repeating characters • Minimum window substring Learning these patterns helps convert brute-force solutions into efficient O(n) algorithms. #DSA #SlidingWindow #Algorithms #LearningInPublic #ProblemSolving
To view or add a comment, sign in
-
🚀 Day 11 of my DSA Revision Journey: Sorting Techniques Today I learned and implemented fundamental Sorting Algorithms — one of the most important concepts in Data Structures & Algorithms. 🔹 Selection Sort • Finds minimum element and places it at correct position • Time Complexity: O(n²) • Space Complexity: O(1) 🔹 Bubble Sort • Pushes largest element to the end in each round • Best Case: O(n) (optimized) • Worst Case: O(n²) 🔹 Insertion Sort • Inserts elements into correct position like sorting cards • Best Case: O(n) • Worst Case: O(n²) 💡 Key Learning: ✔ Concept of stable vs unstable sorting ✔ In-place sorting techniques ✔ Comparing time complexities of different approaches Sorting is a foundation for many advanced algorithms — great learning today! #DSA #SortingAlgorithms #LearningInPublic #CodingJourney #PlacementPreparation
To view or add a comment, sign in
-
-
🚀 DSA Consistency Challenge – Day 22 Today I solved two interesting number-based problems that focus on detecting patterns in numbers and avoiding infinite loops. 1️⃣ Happy Number A number is called happy if repeatedly replacing the number with the sum of the squares of its digits eventually results in 1. If the process falls into a cycle that never reaches 1, the number is not happy. Approach 1: Brute Force (Using Set) Start with the number n. Compute the sum of the squares of its digits. Keep repeating the process. Store every intermediate number in a set. If we reach 1 → Happy Number. If a number repeats (already in the set) → a cycle exists, so it is not happy. Time Complexity: O(log n) per transformation, repeated until a cycle → roughly O(log n) Space Complexity: O(log n) due to storing visited numbers in a set. Approach 2: Optimal (Floyd’s Cycle Detection / Fast & Slow Pointers) Instead of storing numbers, treat the sequence as a cycle detection problem. Use two pointers: Slow pointer: moves one transformation at a time Fast pointer: moves two transformations at a time If the number is not happy, the sequence will eventually form a cycle, and both pointers will meet. If the sequence reaches 1, the number is happy. Time Complexity: O(log n) Space Complexity: O(1) (no extra memory used). 2️⃣ Ugly Number An ugly number is a positive number whose prime factors are only 2, 3, and 5. Approach 1: Brute Force (Prime Factorization) Find the prime factors of the number by checking divisibility up to √n. While dividing the number by its factors, check whether the factor is 2, 3, or 5. If any other prime factor appears → the number is not ugly. Time Complexity: O(√n) Space Complexity: O(1) Approach 2: Optimal (Repeated Division) Continuously divide the number by 2, 3, and 5 whenever possible. These are the only allowed prime factors. After removing all possible 2s, 3s, and 5s: If the remaining number is 1 → Ugly Number Otherwise → Not Ugly Time Complexity: O(log n) Space Complexity: O(1) 📌 Key Learning Today Some problems that look like math problems are actually cycle detection problems. Recognizing patterns like Floyd’s algorithm or prime factor reduction can turn a brute force solution into an efficient one.
To view or add a comment, sign in
-
DSA taught me something important. Sometimes the solution fails - TLE. Wrong Answer. Edge case missed. But every failure shows where the thinking was incomplete. Algorithms aren’t just about code. They train you to debug your logic, improve your approach, and try again -smarter. Consistency > Perfection. #DSA #Algorithms #ProblemSolving #LearningJourney
To view or add a comment, sign in
-
My DSA Journey – Day 12 📚 Today’s Focus: Pair Sum & Moore’s Voting Algorithm • Solved the Pair Sum problem using a brute-force approach • Optimized it for better time complexity • Learned Moore’s Voting Algorithm • Implemented both brute-force and optimized solutions 🧠 Key Learning: Today was about understanding patterns and reducing unnecessary computations. For Pair Sum, I learned how changing the approach can significantly reduce time complexity. Moore’s Voting Algorithm was interesting — instead of using extra space, it smartly tracks the majority element in linear time O(n) with constant space O(1). varrenya panuganti #DSA #Algorithms #MooresVoting #ProblemSolving #TimeComplexity #CodingJourney #LearningInPublic
To view or add a comment, sign in