Solved 719. Find K-th Smallest Pair Distance with Binary Search

🚀 Day 80 of My #100DaysOfLeetCode Journey Problem: 719. Find K-th Smallest Pair Distance Difficulty: Hard 💥 Today I tackled another challenging problem that really tested my ability to optimize beyond brute force. At first, I wrote a simple solution: 👉 Generate all possible pairs 👉 Find their absolute differences 👉 Sort them and return the (k-1)th smallest It worked fine — until the 11th test case 😅 — Time Limit Exceeded (TLE) reminded me that brute force doesn’t scale! Then I explored the efficient approach: 🔹 Sort the array 🔹 Use binary search on the distance range (0 to max diff) 🔹 For each mid distance, use two pointers to count how many pairs have a distance ≤ mid This reduced the time complexity drastically and helped me understand how binary search can be used not only on arrays, but also on the answer space itself! 💡 Key Learnings: Think about what you’re searching for — sometimes the answer itself is the search space. Two-pointer patterns are incredibly powerful when combined with sorted data. Optimization is not just about speed — it’s about clarity and scalability. Every hard problem starts as a puzzle, but once you see the pattern, it becomes a story of logic. #LeetCode #100DaysOfCode #Java #ProblemSolving #BinarySearch #TwoPointers #CodingJourney

  • graphical user interface, text, application, chat or text message

To view or add a comment, sign in

Explore content categories