DUET PG Score Card 2021
NHAI Recruitment 2021
Bihar Board 12th Dummy Admit Card 2022
RBI Assistant Notification 2021
Jee Advanced 2021 Result
NIACL Assistant Recruitment 2021
SBI PO Online Form 2021

Max Consecutive Ones Leetcode Solution

Given a binary array nums, return the maximum number of consecutive 1‘s in the array.

Example 1:

Input: nums = [1,1,0,1,1,1]
Output: 3
Explanation: The first two digits or the last three digits are consecutive 1s. The maximum number of consecutive 1s is 3.

Example 2:

Input: nums = [1,0,1,1,0,1]
Output: 2

Constraints:

  • 1 <= nums.length <= 105
  • nums[i] is either 0 or 1.
class Solution {
    public static int findMaxConsecutiveOnes(int[] nums) {
        // Hint: Initialise and declare a variable here to
        int count = 0,maxV=0;
        // keep track of how many 1's you've seen in a row.
        for (int i = 0; i < nums.length; i++) {
            // Do something with element nums[i].
            if (nums[i] == 1) {
                count++;
                 maxV=Math.max(maxV,count);
            }
            else{
                count=0;;
            }

        }
        return maxV;
    }
}

Candidates having any kind of query so he/she can ask in our comment section. Our Panel will b reverting back as soon as possible. Thank You. For more details visit our website (https://speedjobalert.com/)

Leave a Comment