Rotate Non-Negative Elements in Array

📅 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

  • text

To view or add a comment, sign in

Explore content categories