
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
Finding maximum number of consecutive 1's in a binary array in JavaScript
We are required to write a JavaScript function that takes in a binary array (an array that consists of 0 or 1 only) as the only argument.
The function should find the length of that consecutive subarray of the array that consists of only 1 and return it.
For example −
If the input array is −
const arr = [1, 0, 1, 1, 1, 0, 1, 0, 1, 1, 1, 1, 0, 1];
Then the output should be −
const output = 4;
We will use the sliding window algorithm to capture the largest window (largest in size) that consists of only 1.
Example
The code for this will be −
const arr = [1, 0, 1, 1, 1, 0, 1, 0, 1, 1, 1, 1, 0, 1]; const findMaxConsecutiveOnes = (arr = []) => { let left = 0; let right = 0; let max = 0; while (right < arr.length) { if (arr[right] === 0) { if (right - left > max) { max = right - left }; right++; left = right; } else { right++ }; }; return right - left > max ? right - left : max; } console.log(findMaxConsecutiveOnes(arr));
Output
And the output in the console will be −
4
- Related Articles
- Maximum length of consecutive 1’s in a binary string in Python using Map function
- Count number of binary strings without consecutive 1's in C
- Finding the first non-consecutive number in an array in JavaScript
- JavaScript Finding the third maximum number in an array
- Program to Count number of binary strings without consecutive 1’s in C/C++?
- Finding the third maximum number within an array in JavaScript
- Python program to check if there are K consecutive 1’s in a binary number?
- C# program to check if there are K consecutive 1’s in a binary number
- Finding the maximum in a nested array - JavaScript
- C/C++ Program to Count number of binary strings without consecutive 1’s?
- Maximum consecutive one’s (or zeros) in a binary circular array in C++
- Count the number of 1’s and 0’s in a binary array using STL in C++
- Javascript Program to Count 1’s in a sorted binary array
- Count Binary String without Consecutive 1's
- Maximum sum of n consecutive elements of array in JavaScript

Advertisements