Solved LeetCode Problem 25 with Cyclic Sort in Java

Problem 25 : Leetcode 🚀 LeetCode Problem Solved: Find the Duplicate Number (Cyclic Sort Approach) 🧠 🔍 Problem Overview: Given an array of n+1 integers where each number is between 1 and n (inclusive), at least one duplicate number must exist. The task: Find that duplicate — with O(n) time and O(1) extra space. 💡 My Approach – Cyclic Sort Technique: I applied the Cyclic Sort algorithm to achieve both performance and space efficiency. In-Place Hashing Logic: I treated the array as a hash map where each number x should ideally be placed at index x - 1. Duplicate Detection: If arr[i] != arr[arr[i] - 1], I swapped the elements to place them correctly. If arr[i] == arr[arr[i] - 1], it indicates that arr[i] is the duplicate number — and I immediately returned it. 🏁 Results: ✅ Time Complexity: O(n) – Runtime: 6 ms, beating 50.29% of Java submissions. ✅ Space Complexity: O(1) – Solved entirely in-place. ✅ Memory Usage: 82.82 MB, outperforming 6.05% of other submissions. 🧩 Tech Insight: Cyclic Sort is an elegant in-place sorting strategy ideal for problems involving numbers within a fixed range — making it a powerful tool for efficient duplicate detection and data placement. #LeetCode #Java #DSA #ProblemSolving #CodingJourney #SpringBootDeveloper #BackendDevelopment #CyclicSort #JavaDeveloper #LearningInPublic Link : https://lnkd.in/diWSfAaM

  • No alternative text description for this image

To view or add a comment, sign in

Explore content categories