-
Notifications
You must be signed in to change notification settings - Fork 1
/
max_series_of_1.c
57 lines (55 loc) · 987 Bytes
/
max_series_of_1.c
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
//program to print max series of one
/*
input :
10
0 1 1 1 0 0 1 1 1 0
2
output
maximum =5
index are given below
0 1 2 3 4
*/
#include<stdio.h>
#include<stdlib.h>
int main()
{
int n;
scanf("%d",&n);
int *a=(int *)malloc(sizeof(int)*n);
for(int i=0;i<n;i++)
scanf("%d",&a[i]);
int n1;
scanf("%d",&n1);
int r=0,l=0,posr=0,max=0,count_zero=0;
while(r<n)
{
if(a[r]==1)
r++;
else if(a[r]==0&&count_zero<n1)
{
r++;
count_zero++;
}
else
{
if(max<r-l)
{
max=r-l;
posr=r;
}
while(a[l]!=0)
l++;
l++;
count_zero--;
}
}
if(max<r-l)
{
max=r-l;
posr=r;
}
printf("Maximum is %d \n", max);
printf("index are given below\n\n");
for(int i=posr-max;i<posr;i++)
printf("%d",i);
}