So today I’ll solve some basic array questions as follows and I’ll share the code that I have done. Let’s get started on my day 2 journey.
Find the Missing number in the array #1
for this question I have implemented the logic as follows, as it it a very basic and straightforward question.
//122 / 122 testcases passed
/**
* @param {number[]} nums
* @return {number}
*/
var missingNumber = function (nums) {
if (nums.length === 0) return;
nums = nums.sort((a, b) => a - b)
for (let i = 0; i <= nums.length; i++) {
if (nums[i] !== i) {
return i;
}
}
};
Max Consecutive Ones #2 — Leetcode — 485
Given a binary array nums, return the maximum number of consecutive 1's in the array.
/**
* @param {number[]} nums
* @return {number}
*/
var findMaxConsecutiveOnes = function (nums) {
if (nums.length === 0) return;
let counter = 0;
let max = 0;
for (let i = 0; i < nums.length; i++) {
if (nums[i] === 1) {
counter++;
if (counter > max) {
max = counter
}
} else {
counter = 0
}
}
return max
};
Thanks for visiting
Bidhubhushan Gahan
7609074970
Github - https://github.com/Bidhu1024
LinkedIn - https://www.linkedin.com/in/bidhu05/
Leetcode - https://leetcode.com/u/Bidhubhushan_Gahan/
Top comments (0)