shriyanshi24jindal

Max Consecutive Ones

Mar 30th, 2026
22
0
Never
Not a member of GistPad yet? Sign Up, it unlocks many cool features!
Java 3.07 KB | None | 0 0
  1. class Solution {
  2. public int findMaxConsecutiveOnes(int[] nums) {
  3. int max = 0;
  4. int consecutive_ones = 0;
  5. for(int i=0;i<nums.length;i++)
  6. {
  7. if(nums[i] == 0)
  8. {
  9. max = Math.max(consecutive_ones, max);
  10. consecutive_ones = 0;
  11. }
  12. else
  13. consecutive_ones++;
  14. }
  15. max = Math.max(consecutive_ones, max);
  16. return max;
  17. }
  18. }
RAW Paste Data Copied