LeetCode 442: Cyclic Sort to Find Array Duplicates

🚀 Day 29 of #100DaysOfCode Solved 442. Find All Duplicates in an Array on LeetCode ✅ 🧠 Key Insight: Since numbers are in the range 1 → n, each number ideally belongs at index num - 1. By repeatedly swapping elements into their correct positions (cyclic sort), duplicates naturally reveal themselves. ⚙️ Approach Used: 🔹Place each element at its correct index (nums[i] → nums[nums[i] - 1]) 🔹Skip when the element is already in the correct position 🔹After rearrangement, any index i where nums[i] ≠ i + 1 is a duplicate ⏱️ Time Complexity: O(n) 📦 Space Complexity: O(1) (excluding output list) #100DaysOfCode #LeetCode #DSA #Arrays #CyclicSort #Java #ProblemSolving #InterviewPrep #LearningInPublic

  • graphical user interface, application

To view or add a comment, sign in

Explore content categories