File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1- import java .util .Scanner ;
1+ class MaxConsecutiveOnes {
2+ public int findMaxConsecutiveOnes (int [] nums ) {
3+ if (nums .length == 0 ){
4+ return 0 ;
5+ }
26
3- class MaxConsecutiveOnes {
4- public static int findMaxConsecutiveOnes (int [] nums ) {
5- int max = 0 ;
6- int count = 1 ;
7- if (nums [0 ]==1 && nums .length ==1 ){
8- return 1 ;
7+ if (nums .length == 1 ){
8+ return nums [0 ];
99 }
10- for (int i = 0 ; i <nums .length -1 ; i ++){
11- if (nums [i ]==nums [i +1 ]){
12- count +=1 ;
10+
11+ int max = 0 ;
12+
13+ int count = 0 ;
14+
15+ for (int i = 0 ; i <nums .length ; i ++){
16+ if (nums [i ] == 1 ){
17+ count += 1 ;
1318 }
1419 else {
15- count = 1 ;
16- }
17- if (count >max ){
18- max = count ;
20+ max = Math .max (max , count );
21+ count = 0 ;
1922 }
23+ max = Math .max (max , count );
2024 }
21- return max ;
22- }
2325
24- public static void main (String [] args ){
25- Scanner input = new Scanner (System .in );
26- int n = input .nextInt ();
27- int [] nums = new int [n ];
28- for (int i =0 ; i <n ; i ++){
29- int e = input .nextInt ();
30- if (e ==1 || e ==0 ){
31- nums [i ] = e ;
32- }
33- }
34- findMaxConsecutiveOnes (nums );
26+ return max ;
3527 }
3628}
You can’t perform that action at this time.
0 commit comments