# Blogs Hub

### by Sumit Chourasia | Jun 19, 2020 | Category :coding | Tags : डेटा-संरचनाarrayआसानalgorithmc-sharpleetcode

#### Duplicate Zeros - Array - Easy - LeetCode - मिनी टीवी

Given a fixed length array arr of integers, 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, do not return anything from your function.

Example 1:

Input: [1,0,2,3,0,4,5,0]

Output: null

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

Example 2:

Input: [1,2,3]

Output: null

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

Note:

1 <= arr.length <= 10000

0 <= arr[i] <= 9

Solution:

``````using System;
using System.Collections.Generic;
using System.Text;

{
public class DuplicateZerosSoln
{
public void DuplicateZeros(ref int[] arr)
{
for (int i = 0; i < arr.Length; i++)
{
if (arr[i] == 0)
{
if(i+1 != arr.Length)
{
for (int j = arr.Length-1; j > i; j--)
{
arr[j] = arr[j-1];
}
}
i++;
}
}
}
}
}
``````

Time Complexity: O(N^2)

Space Complexity: O(1)

Unit Tests:

``````using LeetCode.AskGif.Easy.Array;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Collections.Generic;
using System.Text;

namespace CodingUnitTest.Easy.Array
{
[TestClass]
public class DuplicateZerosSolnTests
{
[TestMethod]
public void DuplicateZerosSoln_First()
{
var arr = new int[] { 1, 0, 2, 3, 0, 4, 5, 0 };
var expected = new int[] { 1, 0, 0, 2, 3, 0, 0, 4 };

new DuplicateZerosSoln().DuplicateZeros(ref arr);
AreEqual(expected, arr);
}

[TestMethod]
public void DuplicateZerosSoln_Second()
{
var arr = new int[] { 1, 2, 3 };
var expected = new int[] { 1, 2, 3 };

new DuplicateZerosSoln().DuplicateZeros(ref arr);
AreEqual(expected, arr);
}

private void AreEqual(int[] res, int[] output)
{
Assert.AreEqual(res.Length, output.Length);
for (int i = 0; i < res.Length; i++)
{
Assert.AreEqual(res[i], output[i]);
}
}
}
}
``````

Contributed By: Sumit Chourasia
Contributed By: Sumit Chourasia
Contributed By: Sumit Chourasia
Contributed By: Sumit Chourasia