Advertisement
vaibhav1906

Max Consecutive Ones | Anjul

May 24th, 2022
927
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.00 KB | None | 0 0
  1. class Solution {
  2. public:
  3.     int findMaxConsecutiveOnes(vector<int>& nums)
  4.     {
  5.             vector<int> vec;                               //vector define
  6.         int i=0;
  7.         while(i<nums.size())
  8.         {                                                  //loop till whole input vector
  9.           int j=0;
  10.                  while(i<nums.size() && nums[i]==1)       // loop to count 1s
  11.                  {
  12.                       j++;                                
  13.                       i++;
  14.                  }
  15.           vec.push_back(j);                              //vector to save no of continous 1
  16.           i++;
  17.         }
  18.        
  19.                                                         // to return maximum in vec
  20.        
  21.         int max=vec[0];                      //return *max_element(vec.begin(),vec.end());
  22.         for(int i=0;i<vec.size();i++)
  23.         {
  24.             if(vec[i]>max)
  25.             {
  26.                 max=vec[i];
  27.             }
  28.         }
  29.         return max;
  30.         }
  31. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement