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.
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:
-
We start by defining a function
duplicateZerosthat takes an arrayarras an argument. -
We initialize a counter
ito 0. This counter will be used to iterate over the array. -
We start a while loop that will continue as long as
iis less than the length of the array. -
Inside the loop, we check if the current element of the array (i.e.,
arr[i]) is 0. -
If it is, we insert a 0 at the current index
iusing theinsertmethod. This shifts all elements to the right. -
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
popmethod. -
We then increment
iby 1 to skip the next 0 we just inserted. -
If the current element is not 0, we simply increment
iby 1 to move to the next element. -
Finally, we return the modified array.
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.
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.