Max Consecutive Ones

🏠 ⬅️ ➡️

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.

Note: This problem is from LeetCode.
Compiled
Executed
Correct
program main
    ! Solves the problem: Given a binary array `nums`, return the maximum number of consecutive `1`s in the array.
    implicit none

    integer, parameter :: n = 6
    integer, dimension(n) :: nums
    integer :: max_consecutive_ones

    ! Examples
    nums = [1, 1, 0, 1, 1, 1]
    write (*, '(A, I0)') 'Example 1: ', max_consecutive_ones(nums)
    nums = [1, 0, 1, 1, 0, 1]
    write (*, '(A, I0)') 'Example 2: ', max_consecutive_ones(nums)

contains

    function max_consecutive_ones(nums) result(max_consecutive_ones_)
        ! Returns the maximum number of consecutive `1`s in the array `nums`.
        implicit none
        integer, dimension(:), intent(in) :: nums
        integer :: max_consecutive_ones_, current_consecutive_ones

        current_consecutive_ones = 0
        max_consecutive_ones_ = 0

        do i = 1, size(nums)
            if (nums(i) == 1) then
                current_consecutive_ones = current_consecutive_ones + 1
            else
                max_consecutive_ones_ = max(max_consecutive_ones_, current_consecutive_ones)
                current_consecutive_ones = 0
            end if
        end do

        max_consecutive_ones_ = max(max_consecutive_ones_, current_consecutive_ones)
    end function max_consecutive_ones
end program main
Compiled
Executed
Correct
module max_consecutive_ones

implicit none

contains

integer function max_consecutive_ones(nums) result(max_consecutive_ones)

! 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 1's 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.

integer, intent(in) :: nums(:)
integer :: i, current_streak, max_streak

max_consecutive_ones = 0
current_streak = 0

do i = 1, size(nums)
    if (nums(i) == 1) then
        current_streak = current_streak + 1
    else
        if (current_streak > max_streak) then
            max_streak = current_streak
        end if
        current_streak = 0
    end if
end do

if (current_streak > max_streak) then
    max_streak = current_streak
end if

end function max_consecutive_ones

end module max_consecutive_ones

program test_max_consecutive_ones

use max_consecutive_ones
implicit none

integer :: nums(6)

nums = [1, 1, 0, 1, 1, 1]
write (*,*) max_consecutive_ones(nums)

nums = [1, 0, 1, 1, 0, 1]
write (*,*) max_consecutive_ones(nums)

end program test_max_consecutive_ones
🌐 Data from online sources
def findMaxConsecutiveOnes(nums):
    max_consecutive, current_consecutive = 0, 0
    for num in nums:
        current_consecutive = current_consecutive + 1 if num == 1 else 0
        max_consecutive = max(max_consecutive, current_consecutive)
    return max_consecutive

The algorithm iterates through the given binary array nums. For each element, it checks if the current number is 1, then it increments the current_consecutive counter; otherwise, it resets the counter to 0. After updating the counter, it compares the current value of current_consecutive with the current maximum consecutive ones count (max_consecutive) and updates the maximum count if the current count is higher. After iterating through the entire array, the algorithm returns the maximum number of consecutive 1's found.

🌐 Data from online sources
int findMaxConsecutiveOnes(vector<int>& nums) {
    int max_consecutive = 0, current_consecutive = 0;
    for (int num : nums) {
        current_consecutive = (num == 1) ? current_consecutive + 1 : 0;
        max_consecutive = max(max_consecutive, current_consecutive);
    }
    return max_consecutive;
}

The algorithm iterates through the given binary array nums. For each element, it checks if the current number is 1, then it increments the current_consecutive counter; otherwise, it resets the counter to 0. After updating the counter, it compares the current value of current_consecutive with the current maximum consecutive ones count (max_consecutive) and updates the maximum count if the current count is higher. After iterating through the entire array, the algorithm returns the maximum number of consecutive 1's found.