Repository navigation
Expand file tree
/
Copy pathproblem1493.java
More file actions
75 lines (43 loc) · 1.43 KB
/
Copy pathproblem1493.java
File metadata and controls
75 lines (43 loc) · 1.43 KB
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
69
70
71
72
//1493. Longest Subarray of 1's After Deleting One Element
// this is the same problem as problem1004 . go through the questions of both and analze , just a little moddification
public class problem1493 {
public static int longestSubarray(int[] nums) {
//--------------------------------------- modified / added code-----------------------
int k=1;
if(nums.length==1)
return 0;
//-------------------------------------------------------------
if(k>=nums.length)
return nums.length;
int c0=0;
int start =0, end=0;
int max=0;
while(end<nums.length)
{
if(nums[end]==0)
{
c0++;
}
if(c0>k)
{
while(c0>k && start<nums.length)
{
if(nums[start]==0 && c0>k)
{
c0--;
}
start++;
}
}
// System.out.println("start :"+start+" end:"+end);
if((end-start+1)>max)
max=(end-start)+1;
end++;
}
return max-1; //modified from "return max" to "return max-1"
}
public static void main(String[] args) {
int [] a = {1,1,0,1};
System.out.println(longestSubarray(a));
}
}