-
Notifications
You must be signed in to change notification settings - Fork 1
/
1089. Duplicate Zeros
59 lines (56 loc) · 1.14 KB
/
1089. Duplicate Zeros
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
//Using extra space
class Solution {
public void duplicateZeros(int[] arr)
{
int n=arr.length;
List<Integer> l = new ArrayList<>();
for(int i:arr)
{
if(i==0)
{
l.add(0);
l.add(0);
}
else
l.add(i);
if(l.size()==n)
break;
}
int i;
for(i=0;i<n;i++)
arr[i]=l.get(i);
}
}
//Without using extra space
class Solution {
public void duplicateZeros(int[] arr)
{
int n=arr.length;
int i,c=0;
List<Integer> l = new ArrayList<>();
for(i=0; i<n;i++)
{
if(arr[i]==0)
c+=2;
else
c++;
if(c>=n)
break;
}
System.out.println(i);
int j=n-1;
if(c>n)
arr[j--]=arr[i--];
while(i>=0)
{
if(arr[i]==0)
{
arr[j--]=0;
arr[j--]=0;
i--;
}
else
arr[j--]=arr[i--];
}
}
}