Maximum Consecutive Ones

Arrays Fundamentals Easy
  • A fun and practical application of this problem exists in data compression techniques, used in various aspects of software development
  • In particular, a variant of this problem is employed in Run-Length Encoding (RLE) that is a simple form of data compression where runs of data are stored as a single data value and count
  • It counts the occurrences of consecutive 1s (or 0s), which is similar to the given problem
  • RLE is used in graphics files formats including BMP, TIFF, and in certain PDF compression schemes

Given a binary array nums, return the maximum number of consecutive 1s in the array.


A binary array is an array that contains only 0s and 1s.

Examples:

Input: nums = [1, 1, 0, 0, 1, 1, 1, 0]

Output: 3

Explanation: The maximum consecutive 1s are present from index 4 to index 6, amounting to 3 1s

Input: nums = [0, 0, 0, 0, 0, 0, 0, 0]

Output: 0

Explanation: No 1s are present in nums, thus we return 0

Input: nums = [1, 0, 1, 1, 1, 0, 1, 1, 1]

Constraints

  • 1 <= nums.length <= 105
  • nums[i] is either 0 or 1.

Hints

  • Traverse the array while keeping a running count of consecutive 1s. Reset the count to 0 whenever a 0 is encountered.
  • Maintain a variable to track the maximum count of consecutive 1s observed during traversal.

Company Tags

Byju's Oracle Mastercard Bain & Company Splunk HashiCorp Reddit Zynga Micron Technology OYO Rooms Roche Activision Blizzard Uber Ernst & Young Nutanix ARM American Express Morgan Stanley Philips Healthcare JPMorgan Chase Pinterest Swiggy Optum Stripe Wayfair TCS Cognizant Accenture Infosys Capgemini Wipro Amazon