LeetCode: In-Place Cyclic Sort for Duplicate Number

🚀 Day 6 of #100DaysOfCode Solved Find the Duplicate Number on LeetCode using an in-place cyclic sort approach 🔁 🧠 Key insight: When numbers are in the range 1…n, each value belongs at index value − 1. If during placement we find that a number is already at its correct position, that number must be the duplicate. ⚙️ Approach: 🔹Iterate through the array 🔹Place each number at its correct index (num − 1) using swaps 🔹If a conflict occurs (same number already present), return it ⏱️ Time Complexity: O(n) 📦 Space Complexity: O(1) (no extra data structures) #100DaysOfCode #LeetCode #DSA #CyclicSort #Java #ProblemSolving #LearningInPublic #CodingJourney

  • graphical user interface, application

To view or add a comment, sign in

Explore content categories