code.ashish.me

Atom feed

Recently added: 128 Longest Consecutive Sequence, 347 Top K Frequent Elements, 045 Jump Game 2, 228 Summary Ranges, 219 Contains Duplicate 2

Maximumconsecutive1Inbinaryarray

/**
 *
 * Ashish Patel
 * e: ashishsushilPatel@gmail.com
 * w: https://ashish.me
 *
 */

class MaximumConsecutive1InBinaryArray {

  static int func(int[] nums){
    int current = 0;
    int max = 0;
    for(int i = 0; i < nums.length; i++){
      if(nums[i] == 1){
        current += 1;
      } else {
        current = 0;
      }
      max = Math.max(current, max);
    }
    return max;
  }

  public static void main(String[] args){
    int[] nums =  { 1,0, 1, 1, 0, 1, 1, 1, 1, 0};
    int result = func(nums);
    System.out.println(result);
  }
}

Created 2021-11-11T00:04:16+00:00 · Edit