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

Note: This problem is from LeetCode.
Compiled
Executed
Correct
program main
    implicit none

    integer, parameter :: n = 8
    integer :: arr(n)

    arr = [1, 0, 2, 3, 0, 4, 5, 0]
    call duplicate_zeros(arr)
    write (*, '(*(I1, 1X))') arr

    arr = [1, 2, 3]
    call duplicate_zeros(arr)
    write (*, '(*(I1, 1X))') arr

contains

subroutine duplicate_zeros(arr)
    implicit none
    integer, intent(inout) :: arr(:)
    integer :: i, j

    do i = 1, size(arr) - 1
        if (arr(i) == 0) then
            do j = size(arr), i + 1, -1
                arr(j) = arr(j - 1)
            end do
            arr(i) = 0
        end if
    end do
end subroutine duplicate_zeros

end program main
Compiled
Executed
Correct
! Declare the input array and its length
integer, intent(inout) :: arr(100000)
integer :: n

! Initialize the length of the input array
n = size(arr)

! Loop through the input array and duplicate each occurrence of zero
do i = 1, n
    if (arr(i) == 0) then
        ! Shift the remaining elements to the right
        do j = i, n - 1
            arr(j) = arr(j + 1)
        end do
        ! Increment the length of the input array
        n = n + 1
    end if
end do

! Print the modified input array to stdout
do i = 1, n
    write (*,*) arr(i)
end do

end
🌐 Data from online sources
def duplicateZeros(arr):
    n = len(arr)
    i = 0
    while i < n - 1:
        if arr[i] == 0:
            for j in range(n - 1, i + 1, -1):
                arr[j] = arr[j - 1]
            arr[i + 1] = 0
            i += 1
        i += 1

The algorithm iterates through the input array arr. When a zero is encountered at position i, a second for loop or equivalent control structure (for, while, or range depending on the language) is used to shift all elements at positions greater than i to the right by one. The element at position i + 1 is then set to zero, and the iterator i is incremented to skip the newly added zero. This process is repeated until the end of the array is reached, ensuring that consecutive zeros are processed correctly.

🌐 Data from online sources
void duplicateZeros(vector<int>& arr) {
    int n = arr.size();
    for (int i = 0; i < n - 1; ++i) {
        if (arr[i] == 0) {
            for (int j = n - 1; j > i + 1; --j) {
                arr[j] = arr[j - 1];
            }
            arr[++i] = 0;
        }
    }
}

The algorithm iterates through the input array arr. When a zero is encountered at position i, a second for loop or equivalent control structure (for, while, or range depending on the language) is used to shift all elements at positions greater than i to the right by one. The element at position i + 1 is then set to zero, and the iterator i is incremented to skip the newly added zero. This process is repeated until the end of the array is reached, ensuring that consecutive zeros are processed correctly.