Max Consecutive Ones

PHOTO EMBED

Sat May 25 2024 11:57:08 GMT+0000 (Coordinated Universal Time)

Saved by @ayushg103 #c++

 int findMaxConsecutiveOnes(vector<int>& nums) {
        int count = 0;
        int m=0;
        int n=nums.size();
        for(int j=0;j<n;++j)
        {
            if(nums[j]==1)
            {
                count++;
                 m=max(count,m);
            }
            else{
               
                count=0;
            }
        }
        return m;
        
    }
content_copyCOPY

Initially you were using m inside else but the prblm is if else does not run fully like [1 1 1] or [1 1 0 0 1 1 1].here ans is 3 and 3 but o/p will be 0 and 2. BEWARE THAT ELSE STATEMENT WILL RUN OR NOT. if(nums[j]==1) { count++; } else{ m=max(count,m); count=0; }