forked from ashoklathwal/Code-for-Interview
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathflip_0_To_1.java
68 lines (57 loc) · 1.73 KB
/
flip_0_To_1.java
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
60
61
62
63
64
65
66
67
68
/*
Naive Approach :
A Simple Solution is to consider every subarray by running two loops. For every subarray,
count number of zeroes in it. Return the maximum size subarray with m or less zeroes.
Time Complexity of this solution is O(n2).
best approch :
The main steps are:
– While zeroCounts is less than or equal to m: expand the window to the right (wR++) and update the count zeroCount.
– While zeroCounts exceeds m, shrink the window from left (wL++), update zeroCount;
– Update the widest window along the way. The positions of output zeros are inside the best window.
*/
class flip_0_To_1
{
public static void flip(int[] arr, int n, int m)
{
int wLIndex = 0; // left index of current window
int wRIndex = 0; // right index of current window
int zeroCounts = 0; // store count of number of zeros in cuurent window
int bestWindow = 0;
int bestLIndex = 0;
while(wRIndex < n)
{
if(zeroCounts <= m)
{
if(arr[wRIndex] == 0)
zeroCounts++;
wRIndex++;
}
if(zeroCounts > m)
{
if(arr[wLIndex] == 0)
zeroCounts--;
wLIndex++;
}
if((wRIndex - wLIndex) > bestWindow)
{
bestWindow = wRIndex - wLIndex;
bestLIndex = wLIndex;
}
}
System.out.print("Indexes of Zeros to be flipped are : ");
for(int i=0; i < bestWindow; i++)
{
if(arr[i + wLIndex] == 0)
System.out.print((i + wLIndex) +" ");
}
System.out.println();
System.out.println("Number of 1's after flipping zeros are : "+ bestWindow);
}
public static void main(String[] args)
{
int[] arr = {1, 0, 0, 1, 1, 0, 1, 0, 1, 1};
int m = 2; // maximum number of zeros allowed to flip
int n = arr.length;
flip(arr, n, m);
}
}