Knowee
Questions
Features
Study Tools

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

Question

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

🧐 Not the exact question you are looking for?Go ask a question

Solution

Sure, here is a Python solution for the problem:

def duplicateZeros(arr):
    i = 0
    while i < len(arr):
        if arr[i] == 0:
            arr.insert(i, 0)
            arr.pop()
            i += 1
        i += 1
    return arr

Here is a step-by-step explanation of the code:

  1. We start by defining a function duplicateZeros that takes an array arr as an argument.

  2. We initialize a counter i to 0. This counter will be used to iterate over the array.

  3. We start a while loop that will continue as long as i is less than the length of the array.

  4. Inside the loop, we check if the current element of the array (i.e., arr[i]) is 0.

  5. If it is, we insert a 0 at the current index i using the insert method. This shifts all elements to the right.

  6. Since the array is of fixed length, we need to remove the last element of the array to maintain the same length. We do this using the pop method.

  7. We then increment i by 1 to skip the next 0 we just inserted.

  8. If the current element is not 0, we simply increment i by 1 to move to the next element.

  9. Finally, we return the modified array.

This problem has been solved

Similar Questions

Given an unsorted array Arr of N positive and negative numbers. Your task is to create an array of alternate positive and negative numbers without changing the relative order of positive and negative numbers.Note: Array should start with a positive number and 0 (zero) should be considered a positive element.

Given an array of N integers containing only 0 or 1. You can do the following operations on the array:swap elements at two indiceschoose one index and change its value from 0 to 1 or vice-versa.You have to do the minimum number of the above operations such that the final array is non-decreasing.Note Consider 1 based Array-indexing

ou have a list arr of all integers in the range [1, n] sorted in a strictly increasing order. Apply the following algorithm on arr:Starting from left to right, remove the first number and every other number afterward until you reach the end of the list.Repeat the previous step again, but this time from right to left, remove the rightmost number and every other number from the remaining numbers.Keep repeating the steps again, alternating left to right and right to left, until a single number remains.Given the integer n, return the last number that remains in arr.Example 1:Input:n = 9Output:6Explanation:arr = [1, 2,3, 4,5, 6,7, 8,9] arr = [2,4, 6,8] arr = [2, 6] arr = [6]Example 2:Input:n = 1Output:1Constraints:- 1 <= n <= 109

Given an array arr of distinct elements of size n, the task is to rearrange the elements of the array in a zig-zag fashion so that the converted array should be in the below form: arr[0] < arr[1]  > arr[2] < arr[3] > arr[4] < . . . . arr[n-2] < arr[n-1] > arr[n]. Note: Modify the given arr[] only, If your transformation is correct, the output will be 1 else the output will be 0

Given an array arr of n positive integers. The task is to swap every ith element of the array with (i+2)th element.

1/3

Upgrade your grade with Knowee

Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.