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 <= 105nums[i] is either 0 or 1.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
temp.f95:17:34:
17 | function max_consecutive_ones(nums) result(max_consecutive_ones_)
| 1
Error: Procedure ‘max_consecutive_ones’ at (1) has an explicit interface from a previous declaration
temp.f95:19:21:
19 | implicit none
| 1
Error: Unexpected IMPLICIT NONE statement in CONTAINS section at (1)
temp.f95:20:49:
20 | integer, dimension(:), intent(in) :: nums
| 1
Error: Unexpected data declaration statement in CONTAINS section at (1)
temp.f95:21:66:
21 | integer :: max_consecutive_ones_, current_consecutive_ones
| 1
Error: Unexpected data declaration statement in CONTAINS section at (1)
temp.f95:23:36:
23 | current_consecutive_ones = 0
| 1
Error: Unexpected assignment statement in CONTAINS section at (1)
temp.f95:24:33:
24 | max_consecutive_ones_ = 0
| 1
Error: Unexpected assignment statement in CONTAINS section at (1)
temp.f95:26:28:
26 | do i = 1, size(nums)
| 1
Error: Unexpected DO statement in CONTAINS section at (1)
temp.f95:27:34:
27 | if (nums(i) == 1) then
| 1
Error: Unexpected block IF statement in CONTAINS section at (1)
temp.f95:28:71:
28 | current_consecutive_ones = current_consecutive_ones + 1
| 1
Error: Unexpected assignment statement in CONTAINS section at (1)
temp.f95:29:16:
29 | else
| 1
Error: Unexpected ELSE statement in CONTAINS section at (1)
temp.f95:30:92:
30 | max_consecutive_ones_ = max(max_consecutive_ones_, current_consecutive_ones)
| 1
Error: Unexpected assignment statement in CONTAINS section at (1)
temp.f95:31:44:
31 | current_consecutive_ones = 0
| 1
Error: Unexpected assignment statement in CONTAINS section at (1)
temp.f95:32:15:
32 | end if
| 1
Error: Expecting END PROGRAM statement at (1)
temp.f95:33:11:
33 | end do
| 1
Error: Expecting END PROGRAM statement at (1)
temp.f95:35:84:
35 | max_consecutive_ones_ = max(max_consecutive_ones_, current_consecutive_ones)
| 1
Error: Unexpected assignment statement in CONTAINS section at (1)
temp.f95:36:7:
36 | end function max_consecutive_ones
| 1
Error: Expecting END PROGRAM statement at (1)
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
temp.f95:7:37:
7 | integer function max_consecutive_ones(nums) result(max_consecutive_ones)
| 1
Error: MODULE attribute of ‘max_consecutive_ones’ conflicts with PROCEDURE attribute at (1)
temp.f95:24:30:
24 | integer, intent(in) :: nums(:)
| 1
Error: Unexpected data declaration statement in CONTAINS section at (1)
temp.f95:25:40:
25 | integer :: i, current_streak, max_streak
| 1
Error: Unexpected data declaration statement in CONTAINS section at (1)
temp.f95:27:21:
27 | max_consecutive_ones = 0
| 1
Error: Symbol ‘max_consecutive_ones’ at (1) has already been host associated
temp.f95:28:18:
28 | current_streak = 0
| 1
Error: Unexpected assignment statement in CONTAINS section at (1)
temp.f95:30:20:
30 | do i = 1, size(nums)
| 1
Error: Unexpected DO statement in CONTAINS section at (1)
temp.f95:31:26:
31 | if (nums(i) == 1) then
| 1
Error: Unexpected block IF statement in CONTAINS section at (1)
temp.f95:32:43:
32 | current_streak = current_streak + 1
| 1
Error: Unexpected assignment statement in CONTAINS section at (1)
temp.f95:33:8:
33 | else
| 1
Error: Unexpected ELSE statement in CONTAINS section at (1)
temp.f95:34:45:
34 | if (current_streak > max_streak) then
| 1
Error: Unexpected block IF statement in CONTAINS section at (1)
temp.f95:35:39:
35 | max_streak = current_streak
| 1
Error: Unexpected assignment statement in CONTAINS section at (1)
temp.f95:36:11:
36 | end if
| 1
Error: Expecting END MODULE statement at (1)
temp.f95:37:26:
37 | current_streak = 0
| 1
Error: Unexpected assignment statement in CONTAINS section at (1)
temp.f95:38:7:
38 | end if
| 1
Error: Expecting END MODULE statement at (1)
temp.f95:39:3:
39 | end do
| 1
Error: Expecting END MODULE statement at (1)
temp.f95:41:37:
41 | if (current_streak > max_streak) then
| 1
Error: Unexpected block IF statement in CONTAINS section at (1)
temp.f95:42:31:
42 | max_streak = current_streak
| 1
Error: Unexpected assignment statement in CONTAINS section at (1)
temp.f95:43:3:
43 | end if
| 1
Error: Expecting END MODULE statement at (1)
temp.f95:45:3:
45 | end function max_consecutive_ones
| 1
Error: Expecting END MODULE statement at (1)
temp.f95:51:5:
51 | use max_consecutive_ones
| 1
Fatal Error: Cannot open module file ‘max_consecutive_ones.mod’ for reading at (1): No such file or directory
compilation terminated.
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.
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.