Find the K-th Largest or Smallest in O(n) using nth_element | Competitive Programming Hack

Subscribers:
92,300
Published on ● Video Link: https://www.youtube.com/watch?v=6m8UJ1KwFR8



Duration: 0:00
2,392 views
290


Hello all, welcome to DSA Shorts with MIK - 12 🎥 ✨
Want to find the n-th largest or smallest element in an array without fully sorting it? 😎
Use nth_element() in C++ and do it in O(n) time! ⚡

In this short, we find the 2nd largest element using a simple, powerful STL function — no full sort, just a smart trick! 🔥

✅ What is nth_element()?
It partially sorts your array so that the element at the n-th position is in its correct place — all elements before it are smaller, and all after are greater (not necessarily sorted).

📌 Syntax:

nth_element(begin(v), begin(v) + idx, end(v));
This puts the element at index idx in its correct position, in O(n) time.

🔧 Perfect for competitive programming when you're short on time but need precision!
Add this STL gem to your toolkit and code smarter. 🚀



📚 My DP Concepts Playlist:    • Roadmap for DP | How to Start DP ? | ...  
📚 My Graph Concepts Playlist:    • Graph Concepts & Qns - 1 : Graph will...  
📚 Segment Tree:    • Segment Tree | Introduction | Basics ...  
📚 Recursion:    • Introduction | Recursion Concepts And...  
📚 Trie:    • Word Search II (Google, Amazon, Meta,...  
📚 Difference Array Technique:    • Introduction | What | How | Differenc...  

🔗 GitHub Interview Prep Rephttps://github.com/MAZHARMIK/Interview_DS_Algo...
📸 Instagrahttps://www.instagram.com/codestorywithmik/k  
📘 Faceboohttps://www.facebook.com/people/codestorywithmik/100090524295846/6  
🐦 Twittehttps://twitter.com/CSwithMIKk  
🔔 Subscribe:    / @codestorywithmik  

╔═╦╗╔╦╗╔═╦═╦╦╦╦╗╔═╗
║╚╣║║║╚╣╚╣╔╣╔╣║╚╣═╣
╠╗║╚╝║║╠╗║╚╣║║║║║═╣
╚═╩══╩═╩═╩═╩╝╚╩═╩═╝

#DSA #CodingTips #nth_element #CompetitiveProgramming
#LearnWithMIK #codestorywithmik #shorts #cpp #nthsimplified
#dsa #leetcode #codinginterview #stlmagic #algorithms
#cpptricks #programming #techreels #cppshorts
#hindicoding #hindiexplanation #codingtricks #cppstl
#fastcode #timecomplexity #cplusplusreels #reelsindia
#techshorts #codeoptimization #coderlife #contentcreation
#jobseekers #100daysofcode #interviewtips #codingchallenge




Other Videos By codestorywithMIK


2025-04-08Minimum Number of Operations to Make Elements in Array Distinct | 2 Approaches | Leetcode 3396 | MIK
2025-04-07Partition Equal Subset Sum | Recursion | Memo | Tree Diagram | Leetcode 416 | codestorywithMIK
2025-04-04Lowest Common Ancestor of Deepest Leaves | 2 Approaches | Leetcode 1123 | codestorywithMIK
2025-04-02Auto-translate the captions to any language | YouTube
2025-04-02Maximum Value of an Ordered Triplet I | Multiple Approaches | Leetcode 2873 | codestorywithMIK
2025-03-30Legends aren't defined by their successes
2025-03-30Find Median of X Sized Subarrays | Weekly Contest 443 | Hint To Qn 4 | Detailed | codestorywithMIK
2025-03-29Partition Labels | 2 Clean Approaches | Leetcode 763 | codestorywithMIK
2025-03-29Apply Operations to Maximize Score | Super Detailed Approach For Beginners | Leetcode 2818 | MIK
2025-03-28Binary Exponentiation | Fast Exponentiation | Detailed For Beginners | codestorywithMIK
2025-03-27Find the K-th Largest or Smallest in O(n) using nth_element | Competitive Programming Hack
2025-03-26Minimum Index of a Valid Split | 2 Approaches | Leetcode 2780 | codestorywithMIK
2025-03-25Minimum Operations to Make a Uni-Value Grid | Detailed Simple Maths Proof | Leetcode 2033 | MIK
2025-03-24Check if Grid can be Cut into Sections | Merge Intervals | Leetcode 3394 | Leetcode 56 | Detailed
2025-03-23Count Days Without Meetings | Simplest Explanation | Leetcode 3169 | codestorywithMIK
2025-03-22Number of Ways to Arrive at Destination | Straight Forward Simple | Leetcode 1976 | codestorywithMIK
2025-03-22Why I Will NEVER Promote Paid DSA Courses! 🚫 The Truth You Need to Know | codestorywithMIK
2025-03-21Count the Number of Complete Components | Multiple Approaches | Leetcode 2685 | codestorywithMIK
2025-03-21Find All Possible Recipes from Given Supplies | Brute Force | Optimal | Leetcode 2115
2025-03-17Longest Nice Subarray | Brute Force | Better | Optimal | Dry Runs | Leetcode 2401 | codestorywithMIK
2025-03-16Divide Array Into Equal Pairs | 5 Approaches | Leetcode 2206 | codestorywithMIK