Isaac Emmanuel
15 Jun 2024•EasyC++ / Python / TypeScript

Max Consecutive Ones

C++, Python and TypeScript solutions for Max Consecutive Ones.

The Max Consecutive Ones solutions are below.

Solution files

Python•max-consecutive-ones/synced-solution.py
def findMaxConsecutiveOnes(nums):
    max_count = 0
    current_count = 0

    for num in nums:
        if num == 1:
            current_count += 1
            max_count = max(max_count, current_count)
        else:
            current_count = 0

    return max_count
C++•max-consecutive-ones/synced-solution.cpp
class Solution {
public:
    int findMaxConsecutiveOnes(vector<int>& nums) {
        int max_count = 0, current_count = 0;

        for (int num : nums) {
            if (num == 1) {
                current_count++;
                max_count = max(max_count, current_count);
            } else {
                current_count = 0;
            }
        }

        return max_count;
    }
};
TypeScript•max-consecutive-ones/synced-solution.ts
function findMaxConsecutiveOnes(nums: number[]): number {
    let maxCount = 0, currentCount = 0;

    for (const num of nums) {
        if (num === 1) {
            currentCount++;
            maxCount = Math.max(maxCount, currentCount);
        } else {
            currentCount = 0;
        }
    }

    return maxCount;
}