Skip to main content
Back to problems
#485
Easy Algorithms

Max consecutive ones

Array
64.6% acceptance
Jan 13, 2026
6792
496
Given a binary array nums, return the maximum number of consecutive 1's in the array.

Solution

Rust
Time O(n)
Space O(1)
LeetCode
solution.rs
impl Solution {
  pub fn find_max_consecutive_ones(nums: Vec<i32>) -> i32 {
    let mut max_count = 0;
    let mut current_count = 0;
    
    for &num in &nums {
      if num == 1 {
        current_count += 1;
        max_count = max_count.max(current_count);
      } else {
        current_count = 0;
      }
    }
    
    max_count
  }
}