"Day 110: Duplicate Zeros in Array with In-Place Algorithm"

🚀 Day 110 of #LeetCode Challenge! Problem: Duplicate Zeros 💡 My Approach: The task is to duplicate every zero in the array in-place, shifting the remaining elements to the right. First, count the total number of zeros to determine the “virtual” extended array size. Then, use two pointers — one (i) for the original array and another (j) for the extended index. Traverse backward: Copy each element from i to j. If it’s a zero, write an additional zero (when within bounds). This avoids overwriting elements and keeps the solution in O(1) extra space. ✨ Example: Input: [1,0,2,3,0,4,5,0] Output: [1,0,0,2,3,0,0,4] ⏱ Time Complexity: O(N) — single pass from the end 💾 Space Complexity: O(1) — done in-place without extra memory 👨💻 GitHub Link: https://lnkd.in/gCuFGw-c #LeetCode #Array #InPlaceAlgorithm #TwoPointers #ProblemSolving #DuplicateZeros #Day110 #DSA #CodingChallenge #C++

  • text

To view or add a comment, sign in

Explore content categories