Algorithm


Problem Name: 1089. Duplicate Zeros

Given a fixed-length integer array arr, duplicate each occurrence of zero, shifting the remaining elements to the right.

Note that elements beyond the length of the original array are not written. Do the above modifications to the input array in place and do not return anything.

 

Example 1:

Input: arr = [1,0,2,3,0,4,5,0]
Output: [1,0,0,2,3,0,0,4]
Explanation: After calling your function, the input array is modified to: [1,0,0,2,3,0,0,4]

Example 2:

Input: arr = [1,2,3]
Output: [1,2,3]
Explanation: After calling your function, the input array is modified to: [1,2,3]

 

Constraints:

  • 1 <= arr.length <= 104
  • 0 <= arr[i] <= 9
 

Code Examples

#1 Code Example with Java Programming

Code - Java Programming


class Solution {
  public void duplicateZeros(int[] arr) {
    int numOfZeros = 0;
    for (int num : arr) {
      numOfZeros += num == 0 ? 1 : 0;
    }
    int i = arr.length - 1;
    int j = arr.length + numOfZeros - 1;
    while (i != j) {
      insert(arr, i, j--);
      if (arr[i] == 0) {
        insert(arr, i, j--);
      }
      i--;
    }
  }
  
  private void insert(int[] arr, int i, int j) {
    if (j  <  arr.length) {
      arr[j] = arr[i];
    }
  }
}
Copy The Code & Try With Live Editor

Input

x
+
cmd
arr = [1,0,2,3,0,4,5,0]

Output

x
+
cmd
[1,0,0,2,3,0,0,4]

#2 Code Example with Javascript Programming

Code - Javascript Programming


const duplicateZeros = function(arr) {
  const len = arr.length
  for (let i = len - 1; i >= 0; i--) {
    if (arr[i] === 0) arr.splice(i, 0, 0)
  }
  while (arr.length > len) {
    arr.pop()
  }
}
Copy The Code & Try With Live Editor

Input

x
+
cmd
arr = [1,0,2,3,0,4,5,0]

Output

x
+
cmd
[1,0,0,2,3,0,0,4]

#3 Code Example with Python Programming

Code - Python Programming


class Solution:
    def duplicateZeros(self, arr: List[int]) -> None:
        """
        Do not return anything, modify arr in-place instead.
        """
        i = 0
        for num in list(arr):
            if i >= len(arr): break
            arr[i] = num
            if not num:
                i += 1
                if i < len(arr):
                    arr[i] = num
            i += 1
Copy The Code & Try With Live Editor

Input

x
+
cmd
arr = [1,2,3]

Output

x
+
cmd
[1,2,3]

#4 Code Example with C# Programming

Code - C# Programming


using System.Collections.Generic;

namespace LeetCode
{
    public class _1089_DuplicateZeros
    {
        public void DuplicateZeros(int[] arr)
        {
            var queue = new Queue < int>();
            for (int i = 0; i  <  arr.Length; i++)
            {
                if (i + queue.Count < arr.Length)
                {
                    queue.Enqueue(arr[i]);
                    if (arr[i] == 0)
                        queue.Enqueue(arr[i]);
                }

                arr[i] = queue.Dequeue();
            }
        }
    }
}
Copy The Code & Try With Live Editor

Input

x
+
cmd
arr = [1,2,3]

Output

x
+
cmd
[1,2,3]
Advertisements

Demonstration


Previous
#1084 Leetcode Sales Analysis III Solution in C, C++, Java, JavaScript, Python, C# Leetcode
Next
#1090 Leetcode Largest Values From Labels Solution in C, C++, Java, JavaScript, Python, C# Leetcode