Skip to content

Commit 6a94712

Browse files
committed
#1089: Duplicate Zeros; solution & tests.
1 parent 3656056 commit 6a94712

File tree

2 files changed

+114
-0
lines changed

2 files changed

+114
-0
lines changed
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
/**
2+
* 1089
3+
* Duplicate Zeros
4+
**
5+
* Given a fixed-length integer array arr,
6+
* duplicate each occurrence of zero,
7+
* shifting the remaining elements to the right.
8+
*
9+
* Note that elements beyond the length of the original array are not written.
10+
* Do the above modifications to the input array in place and do not return anything.
11+
*
12+
* Example 1:
13+
* Input: arr = [1,0,2,3,0,4,5,0]
14+
* Output: [1,0,0,2,3,0,0,4]
15+
* Explanation:
16+
* After calling your function,
17+
* the input array is modified to: [1,0,0,2,3,0,0,4]
18+
*
19+
* Example 2:
20+
* Input: arr = [1,2,3]
21+
* Output: [1,2,3]
22+
* Explanation:
23+
* After calling your function,
24+
* the input array is modified to: [1,2,3]
25+
*
26+
* Constraints:
27+
* • 1 <= arr.length <= 10^4
28+
* • 0 <= arr[i] <= 9
29+
*
30+
* Hint 1:
31+
* This is a great introductory problem
32+
* for understanding and working with the concept of in-place operations.
33+
* The problem statement clearly states that we are to modify the array in-place.
34+
* That does not mean we cannot use another array.
35+
* We just don't have to return anything.
36+
*
37+
* Hint 2:
38+
* A better way to solve this would be without using additional space.
39+
* The only reason the problem statement allows you to make modifications in place
40+
* is that it hints at avoiding any additional memory.
41+
*
42+
* Hint 3:
43+
* The main problem with not using additional memory
44+
* is that we might override elements
45+
* due to the zero duplication requirement of the problem statement.
46+
* How do we get around that?
47+
*
48+
* Hint 4:
49+
* If we had enough space available,
50+
* we would be able to accommodate all the elements properly.
51+
* The new length would be the original length of the array plus the number of zeros.
52+
* Can we use this information somehow to solve the problem?
53+
**
54+
* https://leetcode.com/problems/duplicate-zeros/
55+
***/
56+
57+
namespace Problems;
58+
59+
public class DuplicateZerosSolution
60+
{
61+
public void DuplicateZeros( int[] arr )
62+
{
63+
int left = 0;
64+
int right = arr.Length - 1;
65+
int shiftCount = 0;
66+
67+
while ( left + shiftCount < arr.Length )
68+
{
69+
if ( arr[left] == 0 )
70+
{
71+
arr[right] = 0;
72+
73+
shiftCount++;
74+
right--;
75+
}
76+
77+
left++;
78+
}
79+
80+
left--;
81+
82+
while ( left >= 0 && shiftCount > 0 )
83+
{
84+
if ( arr[left] == 0 )
85+
{
86+
shiftCount--;
87+
}
88+
else
89+
{
90+
arr[left + shiftCount] = arr[left];
91+
arr[left] = 0;
92+
}
93+
94+
left--;
95+
}
96+
}
97+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
using NUnit.Framework;
2+
3+
using Problems;
4+
5+
public class DuplicateZerosSolutionTests
6+
{
7+
[TestCase( new int[] { 1, 0, 2, 3, 0, 4, 5, 0 }, ExpectedResult = new int[] { 1, 0, 0, 2, 3, 0, 0, 4 } )]
8+
[TestCase( new int[] { 1, 2, 3 }, ExpectedResult = new int[] { 1, 2, 3 } )]
9+
[TestCase( new int[] { 0, 0, 0, 0, 0, 0, 0 }, ExpectedResult = new int[] { 0, 0, 0, 0, 0, 0, 0 } )]
10+
[TestCase( new int[] { 1, 5, 2, 0, 6, 8, 0, 6, 0 }, ExpectedResult = new int[] { 1, 5, 2, 0, 0, 6, 8, 0, 0 } )]
11+
public int[] DuplicateZerosTest( int[] arr )
12+
{
13+
new DuplicateZerosSolution().DuplicateZeros( arr );
14+
15+
return arr;
16+
}
17+
}

0 commit comments

Comments
 (0)