🚀 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
Rotating Arrays with In-Place Reverse Technique
More Relevant Posts
-
🚀 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
-
-
🚀 #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 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 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
-
-
#100DaysOfCode 🚀 👉Solved: LeetCode #16 – 3Sum Closest After solving the classic 3Sum problem earlier, this variation pushed me to think slightly differently. Instead of finding an exact triplet sum equal to 0, this time the goal was to find the sum *closest* to a given target. 🔹 Approach: • Sorted the array • Fixed one element at a time • Applied the Two Pointer technique • Continuously tracked the minimum absolute difference Key Insight: Sometimes optimization problems are not about exact matches, but about minimizing deviation. Time Complexity: O(n²) Space Complexity: O(1) This problem strengthened my understanding of: ✔ Two Pointers ✔ Greedy comparison logic ✔ Edge case handling What I learned: Small variations in problem statements can completely change the thinking approach. Classic 3Sum focuses on exact zero. 3Sum Closest focuses on minimizing |sum - target|. #LearnInPublic #DSA #Java #LeetCode #TwoPointers #ProblemSolving #CodingJourney #SoftwareEngineering
To view or add a comment, sign in
-
-
Day 27/100 – LeetCode Challenge 🚀 Problem: Sort List Approach: Applied Merge Sort on the linked list Used slow and fast pointers to find the middle Recursively sorted both halves Merged the sorted halves Time Complexity: O(n log n) Space Complexity: O(log n) (recursion stack) Key takeaway: Merge sort is the most efficient sorting technique for linked lists, as it avoids random-access operations required by other algorithms. #LeetCode #100DaysOfCode #DSA #Java #LinkedList #MergeSort #ProblemSolving
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 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 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
-
-
🚀 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
-
Explore related topics
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