Maximum consecutive 1 in a Binary Array GFG
package datastrusture;
public class Maximum1 {
// Trying
public static int max(int[] arr){
int n = arr.length;
int res = 0;
int count = 0;
for(int i = 0; i < n; i++) {
if(arr[i] == 1) {
count++;
}
res = Math.max(count, res);
if(arr[i] == 0){
count = 0;
}
}
return res;
}
// Maximum consecutive 1 in a Binary Array
public static int getMaximum(int[] arr){
int n = arr.length;
int[] a = new int[n];
int count = 0;
for(int i = 0; i < n; i++) {
if(arr[i] == 1){
count++;
}
if(arr[i] == 0 || i == n - 1){
a[i] = count;
count = 0;
}
}
int max = 0;
for(int i = 0; i < n; i++) {
max = Math.max(max,a[i]);
}
return max;
}
public static void main(String[] args) {
int[] array = {0,0,0};
System.out.println(max(array));
}
}
Comments
Post a Comment