🚀 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
LeetCode: In-Place Cyclic Sort for Duplicate Number
More Relevant Posts
-
🚀 Day 76 of #100DaysOfCode Today I worked on a clean and elegant array manipulation problem — LeetCode: Rotate Array ✅ 📌 Problem Summary Given an array, rotate it to the right by k steps in-place, without using extra space. 🧠 Approach Used: Reverse Technique Instead of shifting elements one by one, I used a smart 3-step reverse strategy: Reverse the entire array Reverse the first k elements Reverse the remaining n − k elements This achieves the rotation efficiently and keeps the solution simple. ⚙️ Complexity ⏱ Time: O(n) 💾 Space: O(1) (in-place) 🔥 Key Learnings Sometimes the best solution is about reordering operations, not adding complexity In-place algorithms are powerful when space constraints matter Reverse logic is a recurring and underrated pattern in array problems Onward to Day 77 🚀 Consistency > Motivation 💪 #100DaysOfCode #LeetCode #Java #DSA #Arrays #ProblemSolving #CodingJourney
To view or add a comment, sign in
-
-
🚀 Day 18 of #100DaysOfCode Solved Remove Duplicates from Sorted List II on LeetCode 🔗 🧠 Key insight: When duplicates appear in a sorted linked list, we need to remove all occurrences, not just one. Using a dummy (sentinel) node makes it easier to handle cases where duplicates start at the head. ⚙️ Approach: 🔹Create a dummy node pointing to the head 🔹Traverse the list with two pointers 🔹If a duplicate sequence is found, skip the entire block 🔹Otherwise, move forward normally ⏱️ Time Complexity: O(n) 📦 Space Complexity: O(1) #100DaysOfCode #LeetCode #DSA #LinkedList #Java #ProblemSolving #LearningInPublic #CodingJourney
To view or add a comment, sign in
-
-
🚀 Day 82 of #100DaysOfCode 🔍 Problem Solved: Find Minimum in Rotated Sorted Array Today I worked on a binary search based problem where we need to find the minimum element in a rotated sorted array in O(log n) time. 💡 Key Insight: If the array is already sorted (nums[low] <= nums[high]), then the first element is the minimum. Otherwise: If left half is sorted → minimum must be in right half If right half is unsorted → minimum lies there By carefully adjusting low and high, we narrow down the search space efficiently. 🧠 What I practiced: Modified Binary Search Identifying sorted halves Avoiding unnecessary comparisons #Java #DSA #BinarySearch #LeetCode #ProblemSolving #CodingJourney
To view or add a comment, sign in
-
-
🚀 Day 14 of #100DaysOfCode Solved Merge Two Sorted Lists on LeetCode 🔗🔀 🧠 Key insight: Using a dummy (sentinel) node simplifies pointer handling and avoids edge cases when building the merged list. ⚙️ Approach: 🔹Initialize a dummy node to act as the start of the merged list 🔹Compare nodes from both lists one by one 🔹Attach the smaller node and move the pointer forward 🔹Append remaining nodes once one list is exhausted ⏱️ Time Complexity: O(n + m) 📦 Space Complexity: O(1) #100DaysOfCode #LeetCode #DSA #LinkedList #Java #ProblemSolving #LearningInPublic #CodingJourney
To view or add a comment, sign in
-
-
🚀 Day 33 of #100DaysOfCode Solved 25. Reverse Nodes in K-Group on LeetCode 🔁 🧠 Key Idea: Reverse nodes of a linked list k at a time, while keeping the remaining nodes unchanged if they are fewer than k. ⚙️ Approach: 🔹Traverse the list to check if k nodes exist. 🔹If fewer than k nodes remain → return the list as it is. 🔹Reverse the current k-group using a helper reverse function. 🔹Recursively apply the same logic to the remaining list. 🔹Connect the reversed group with the result of the recursive call. This approach makes the solution clean and modular by separating the logic of: 🔹Finding k nodes 🔹Reversing a segment 🔹Connecting segments ⏱️ Time Complexity: O(n) 📦 Space Complexity: O(n/k) (due to recursion stack) #100DaysOfCode #LeetCode #DSA #LinkedList #Java #ProblemSolving #CodingJourney #InterviewPrep
To view or add a comment, sign in
-
-
🚀 Day 9 of #100DaysOfCode Solved Search in Rotated Sorted Array II on LeetCode using Modified Binary Search (Handling Duplicates) 🔎 🧠 Key insight: With duplicates present, it’s sometimes impossible to identify the sorted half directly. If nums[left] == nums[mid], we safely move left++ to shrink the search space. ⚙️ Approach: 🔹Apply binary search 🔹Handle duplicates by skipping equal boundary values 🔹Identify sorted half and check if target lies within it 🔹Continue search accordingly ⏱️ Time Complexity: O(log n) average, O(n) worst case (due to duplicates) 📦 Space Complexity: O(1) #100DaysOfCode #LeetCode #DSA #BinarySearch #Java #ProblemSolving #LearningInPublic #CodingJourney
To view or add a comment, sign in
-
-
🚀 #100DaysOfCode – Day 12 Solved Search in Rotated Sorted Array II on LeetCode 🔄🔍 🧠 Key insight: When duplicates are present, it’s not always possible to directly identify the sorted half. If nums[left] == nums[mid], we can safely move left++ to reduce the search space and continue. ⚙️ Approach: 🔹Use modified binary search 🔹Handle duplicates by skipping equal boundary elements 🔹Identify the sorted half 🔹Check if the target lies in that range and adjust pointers accordingly ⏱️ Time Complexity: Average: O(log n) Worst case (many duplicates): O(n) 📦 Space Complexity: O(1) #100DaysOfCode #LeetCode #DSA #BinarySearch #Java #ProblemSolving #LearningInPublic #CodingJourney
To view or add a comment, sign in
-
-
🚀 Day 22 of #100DaysOfCode Solved 34. Find First and Last Position of Element in Sorted Array on LeetCode 🔍 🧠 Key insight: To find both boundaries of a target in a sorted array, a single binary search isn’t enough. Running two binary searches—one for the leftmost and one for the rightmost occurrence—gives an optimal solution. ⚙️ Approach: 🔹Perform a binary search to find the first occurrence 🔹Perform another binary search to find the last occurrence 🔹Adjust search boundaries after finding the target to expand left/right ⏱️ Time Complexity: O(log n) 📦 Space Complexity: O(1) #100DaysOfCode #LeetCode #DSA #BinarySearch #Java #ProblemSolving #LearningInPublic #CodingJourney
To view or add a comment, sign in
-
-
🚀 Day 24 of #100DaysOfCode Solved 167. Two Sum II – Input Array Is Sorted on LeetCode 🔢👉👈 🧠 Key insight: Because the array is already sorted, we can avoid hash maps and use a two-pointer approach to find the target sum efficiently. ⚙️ Approach: 🔹Initialize two pointers at the start and end of the array 🔹Calculate the sum of values at both pointers 🔹If the sum is too large → move the right pointer left 🔹If the sum is too small → move the left pointer right 🔹Stop when the target is found ⏱️ Time Complexity: O(n) 📦 Space Complexity: O(1) #100DaysOfCode #LeetCode #DSA #TwoPointers #Java #ProblemSolving #LearningInPublic #CodingJourney
To view or add a comment, sign in
-
-
🚀 Day 23/100 – LeetCode Challenge Today’s problem: Partitioning Into Minimum Number of Deci-Binary Numbers 🔹 Key Insight: The minimum number of deci-binary numbers required is equal to the maximum digit present in the string. 🔹 Approach: Traverse through each character in the string Convert it to integer (ch - '0') Track the maximum digit Return the maximum value 🔹 Time Complexity: O(n) 🔹 Space Complexity: O(1) ✨ Simple logic, but powerful observation! Instead of constructing numbers, we just analyze the digits. Consistency > Motivation 💪 #Day23 #100DaysOfCode #LeetCode #Java #ProblemSolving #CodingJourney
To view or add a comment, sign in
-
Explore content categories
- Career
- Productivity
- Finance
- Soft Skills & Emotional Intelligence
- Project Management
- Education
- Technology
- Leadership
- Ecommerce
- User Experience
- Recruitment & HR
- Customer Experience
- Real Estate
- Marketing
- Sales
- Retail & Merchandising
- Science
- Supply Chain Management
- Future Of Work
- Consulting
- Writing
- Economics
- Artificial Intelligence
- Employee Experience
- Workplace Trends
- Fundraising
- Networking
- Corporate Social Responsibility
- Negotiation
- Communication
- Engineering
- Hospitality & Tourism
- Business Strategy
- Change Management
- Organizational Culture
- Design
- Innovation
- Event Planning
- Training & Development