
- 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
Maximum consecutive 1s after n swaps in JavaScript
Problem
We are required to write a JavaScript function that takes in a binary arr (array that contains only 0 or 1), arr, as the first argument, and a number, num, as the second argument.
We can change at most num 0s present in the array to 1s, and our function should return the length of the longest (contiguous) subarray that contains only 1s after making these changes.
For example, if the input to the function is −
const arr = [1, 1, 1, 0, 0, 0, 1, 1, 1, 1, 0]; const num = 2;
Then the output should be −
const output = 6;
Output Explanation
Because after changing two zeros to 1, the last 6 elements of the array will be 1.
Example
The code for this will be −
const arr = [1, 1, 1, 0, 0, 0, 1, 1, 1, 1, 0]; const num = 2; const longestOnes = (arr = [], num = 1) => { let max = 0; let left = 0; let curr = num; for(let right = 0; right < arr.length; right++){ if(arr[right] === 0){ curr -= 1; }; while(curr < 0){ if(arr[left] === 0){ curr += 1; }; left += 1; }; max = Math.max(max, right - left + 1); }; return max; }; console.log(longestOnes(arr, num));
Output
And the output in the console will be −
6
- Related Articles
- Maximum sum of n consecutive elements of array in JavaScript
- 1 to n bit numbers with no consecutive 1s in binary representation?
- Find consecutive 1s of length >= n in binary representation of a number in C++
- Count number of 1s in the array after N moves in C
- C++ program to find maximum distance between two rival students after x swaps
- Program to find longest consecutive run of 1s in binary form of n in Python
- N consecutive odd numbers JavaScript
- Counting the number of 1s upto n in JavaScript
- Program to find minimum swaps needed to group all 1s together in Python
- Program to find remainder after dividing n number of 1s by m in Python
- Find the number of binary strings of length N with at least 3 consecutive 1s in C++
- Program to count number of swaps required to group all 1s together in Python
- Largest permutation after at most k swaps in C++
- Longest string consisting of n consecutive strings in JavaScript
- Program to find minimum adjacent swaps for K consecutive ones in Python

Advertisements