-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathyomun.c
40 lines (31 loc) · 881 Bytes
/
yomun.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
#include <stdio.h>
int main() {
int n;
printf("Enter the value of n: ");
scanf("%d", &n);
int array[n];
for (int i = 0; i < n; i++) {
printf("Enter element %d (0 or 1): ", i + 1);
scanf("%d", &array[i]);
// Make sure the input is either 0 or 1
if (array[i] != 0 && array[i] != 1) {
printf("Invalid input. Please enter 0 or 1.\n");
return 1;
}
}
int longestRun = 0;
for (int i = 0; i < n; i++) {
if (array[i] == 1) {
int currentRun = 1;
while (i + 1 < n && array[i + 1] == 1) {
currentRun++;
i++;
}
if (currentRun > longestRun) {
longestRun = currentRun;
}
}
}
printf("The length of the longest run of 1s is: %d\n", longestRun);
return 0;
}