Hello everyone, this is my first post here and I don't know if this is the right place to do so but, I have a simple question on leetcode: Loading... , basically for any vector of 0 and 1 you need to return an integer corresponding to the number of simultaneous 1. ex: for [0,1,0,1,1,0,1,1] the correct answer is 2.

My code seems to compile but gives 0. what do I miss here?

fn main() {
println!("{}", find_max_consecutive_ones([0,1,1,0,0,1,1,0,1,0].to_vec()));
}
pub fn find_max_consecutive_ones(nums: Vec<i32>) -> i32 {
let mut counter = 0;
for num in nums {
if num == 1 && num+1 == 1 {
counter += 1;
}}
return counter;
}

You don't need to check adjacent numbers in any one loop iteration. It's sufficient to only look at the current number in isolation and check whether it's a 1 or a 0:

If it's a 1, increment the running count.

If it's a 0, reset the running count to 0.

You also need to keep track of the highest value the running count reaches. That's the trickiest part. There might be an edge case or two that trips you up, depending on how you go about it.

This is almost correct. It doesn't handle the case where the longest sequence of 1's is at the end and there's no trailing 0. For example: [1] or [1, 0, 1, 1].

There's nothing more deep here. The variable num is the induction variable of the for loop, hence it gets successive elements of the vector being iterated over. If you add 1 to it, well, then you added 1 to it. If it was a 0, you get a 1, if it was a 1, you get a 2. You don't get the next element of the vector by adding 1 to the current element. (You would get the next index by adding 1 to the current index, but your code doesn't deal with any indices in any way.)