📅 Day 80 of #100DaysOfLeetCode 🧩 Problem: 3819. Rotate Non-Negative Elements Difficulty: Medium 💡 Key Insight Negative elements must act like fixed blockers. So instead of rotating the entire array, we: Extract only non-negative elements Rotate that extracted list Put them back only into non-negative positions This avoids disturbing any negative indices 🚫. 🛠️ Approach Count how many non-negative elements exist (pc). Reduce k using k % pc to avoid unnecessary rotations. Store all non-negative elements in a temporary array. Perform left rotation using the reverse array technique: Reverse first k elements Reverse remaining elements Reverse the whole array Traverse the original array and replace only non-negative positions with rotated values. ⏱️ Complexity Time: O(n) Space: O(pc) #LeetCode #Java #ProblemSolving #CodingChallenge #100DaysOfCode #DSA #LearningEveryday
Rotate Non-Negative Elements in Array
More Relevant Posts
-
🚀 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 60/100 – LeetCode Challenge ✅ Problem: #67 Add Binary Difficulty: Easy Language: Java Approach: Reverse Iteration with Carry Time Complexity: O(max(n, m)) Space Complexity: O(max(n, m)) Key Insight: Binary addition follows same rules as decimal: Sum bits + carry Result bit = sum % 2 New carry = sum / 2 Solution Brief: Iterated from rightmost bits of both strings. Tracked carry and built result from right to left using StringBuilder. Reversed final string for correct order. #LeetCode #Day60 #100DaysOfCode #Binary #Java #Algorithm #CodingChallenge #ProblemSolving #AddBinary #EasyProblem #StringManipulation #BitManipulation #DSA
To view or add a comment, sign in
-
-
📅 Day 90 of #100DaysOfLeetCode Problem: 3634. Minimum Removals to Balance Array Difficulty: Medium Key Insight: Instead of deciding which elements to remove, focus on keeping the largest possible balanced subarray where max ≤ min × k. Minimum removals = total elements − size of this subarray. Approach: Sort the array to make min/max tracking easy Use a sliding window Expand the right pointer Shrink the left pointer whenever nums[j] > k × nums[i] Track the maximum valid window length Complexity: Time: O(n log n) Space: O(1) #LeetCode #Java #ProblemSolving #CodingChallenge #100DaysOfCode #DSA #LearningEveryday
To view or add a comment, sign in
-
-
🚀 Day 15 of #100DaysOfCode Solved Reverse Linked List on LeetCode 🔄🔗 🧠 Key insight: Reversing a linked list is all about careful pointer manipulation. By tracking prev, current, and next, we can reverse links in-place without extra memory. ⚙️ Approach: 🔹Initialize prev = null, current = head 🔹Store next node before breaking the link 🔹Reverse current.next to point to prev 🔹Move pointers forward until the list ends ⏱️ 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 3 of Daily DSA 🚀 Solved LeetCode 1: Two Sum Approach: Used a HashMap to store numbers with their indices. For each element, checked if the complement (target - current) already exists. Complexity: • Time: O(n) • Space: O(n) Performance: Runtime: 2 ms (Beats 99.15%) Memory: 47.34 MB Focusing on writing clean and efficient solutions before over-optimizing. Consistency > Intensity 💯 #DSA #LeetCode #Java #ProblemSolving #Consistency
To view or add a comment, sign in
-
-
🔹 Day 85 – #100DaysOfLeetCode Problem: 3010. Divide an Array Into Subarrays With Minimum Cost I Difficulty: Easy Key Insight: The cost of a subarray depends only on its first element. Since the first subarray always starts at index 0, the problem reduces to selecting the two smallest possible starting elements from the remaining array. Approach: Fix the first subarray cost as nums[0] Find the smallest and second smallest values in nums[1…n-1] Add them to get the minimum total cost Time Complexity: O(n) Space Complexity: O(1) #LeetCode #Java #ProblemSolving #CodingChallenge #100DaysOfCode #DSA #LearningEveryday
To view or add a comment, sign in
-
-
🚀 Day 99 of #100DaysOfLeetCode 📌 Problem: 67. Add Binary 📊 Difficulty: Easy 💡 Key Insight: Binary addition follows the same rules as decimal addition — add digits from right to left while maintaining a carry. The only difference is everything is in base 2. 🛠️ Approach: Use two pointers starting from the end of both strings Add corresponding bits along with a carry Append the result bit (sum % 2) Update carry (sum / 2) Reverse the final string to get the answer ⏱️ Complexity: Time: O(n) Space: O(n) #LeetCode #Java #ProblemSolving #CodingChallenge #100DaysOfCode #DSA #LearningEveryday
To view or add a comment, sign in
-
-
🚀 Day 13 of #100DaysOfCode Solved Remove Linked List Elements on LeetCode 🔗 🧠 Key insight: While traversing a linked list, careful pointer updates are needed—especially when the head node itself matches the value to be removed. ⚙️ Approach: 🔹Handle cases where the head contains the target value 🔹Traverse the list using a pointer 🔹Skip nodes whose value matches the given target by adjusting next pointers ⏱️ 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 91 of #100DaysOfLeetCode Problem: 1653. Minimum Deletions to Make String Balanced Difficulty: Medium Key Insight: A balanced string must have all 'a's before all 'b's. Any 'b' before an 'a' creates a violation. Approach: Preprocess the string using: - prefixB[i]: number of 'b's strictly before index i - suffixA[i]: number of 'a's strictly after index i For every index i, treat it as a split point and compute: deletions = prefixB[i] + suffixA[i] The minimum over all splits is the answer. Time Complexity: O(n) Space Complexity: O(n) #LeetCode #Java #ProblemSolving #CodingChallenge #100DaysOfCode #DSA #LearningEveryday
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
-
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