
- 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
Contiguous subarray with 0 and 1 in JavaScript
Problem:
We are required to write a JavaScript function that takes in a binary array, arr, (an array that only consists of 0 or 1). Our function should return the length of the contiguous subarray from the array that consists of the same number of 1 and 0.
For example, if the input to the function is −
const arr = [1, 0, 0, 1, 0, 1, 0, 0];
Then the output should be −
const output = 6;
Output Explanation
The first 6 elements of the array are 1, 0, 0, 1, 0, 1 (three 1s and three 0s)
Example
The code for this will be −
const arr = [1, 0, 0, 1, 0, 1, 0, 0]; const findMaxLength = (arr = []) => { const { length } = arr; if (length < 2){ return 0 }; const map = new Map(); map.set(0, -1); let sum = 0; let max = 0; for (var i = 0; i < length; i++) { sum += arr[i] === 0 ? -1 : 1; if (map.has(sum)) { max = Math.max(max, i - map.get(sum)); } else { map.set(sum, i); }; }; return max; }; console.log(findMaxLength(arr));
Code Explanation
Here, we thought of 0 as -1 and 1 as 1, and calculated the sum for different windows, when sum is 0, we knew that subarray must have the same number of 0 and 1.
Output
And the output in the console will be −
6
- Related Articles
- Maximum contiguous sum of subarray in JavaScript
- Largest Sum Contiguous Subarray
- Check for Subarray in the original array with 0 sum JavaScript
- C/C++ Program for Largest Sum Contiguous Subarray?
- JavaScript Strings: Replacing i with 1 and o with 0
- Program to find maximum product of contiguous subarray in Python
- JavaScript Program to Find if there is a subarray with 0 sum
- Maximum length subarray with difference between adjacent elements as either 0 or 1 in C++
- C++ program to find maximum of each k sized contiguous subarray
- Subarray with the greatest product in JavaScript
- Subarray pairs with equal sums in JavaScript
- Longest subarray with unit difference in JavaScript
- Forming string using 0 and 1 in JavaScript
- Find if there is a subarray with 0 sum in C++
- Subarray sum with at least two elements in JavaScript

Advertisements