How to Solve LeetCode 1539: Kth Missing Positive Number in JavaScript

How to Solve LeetCode 1539: Kth Missing Positive Number in JavaScript

A Step-by-Step Guide with Code Walk Through In this Leetcode problem, we’re told that we’re given an ordered array of positive integers that has some missing numbers as input, along with a number (k). Our task: find the kth missing number in that input array. Sounds more complicated than it is. Let’s get at it.

A Step-by-Step Guide with Code Walk Through

In this Leetcode problem, we’re told that we’re given an ordered array of positive integers that has some missing numbers as input, along with a number (k). Our task: find the kth missing number in that input array. Sounds more complicated than it is. Let’s get at it.

Let’s say we have an array with the number [1, 2, 3, 4], and we’re told to find the 2nd missing number. Confusing. Doesn’t seem like there is a missing number in that array.

We’d have to assume that the two missing number are 5 and 6, and thus, the 2nd missing number is 6, and the full array would actually be [1, 2, 3, 4, 5, 6].

What’s the Algorithm?

As a human, if you’re tasked to find the kth missing numbers in a sequence, I’d argue you’d do something like,

  1. Start from 1, counting up
  2. When a number seems to be missing, make note of it by incrementing an internal counter.
  3. When you’ve reached the number of missing numbers you were asked to find, stop, and say which number should’ve been there.

Coding a Solution

Let’s initialize a function called findKthPositive **that takes in an array (arr) and a number (k) as input. And, as described above, we’re going to need to count how many numbers are missing in our sequence, so let’s initialize a **missingCounter variable:

function findKthPositive(arr, k) {
  let missingCounter = 0;
}

Next, we have to walk through our sequence. Let’s do that with a for loop. Typically we setup a for loop to run the entire length of an array. But what do we do when our array has missing values?

leetcode javascript

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

Hire Dedicated JavaScript Developers -Hire JavaScript Developers

Hire dedicated JavaScript Developers who are proficient in AngularJS, ReactJS, NodeJS, & VueJS frameworks. Get flexible hiring models as per your business requirements.

JavaScript Shopping Cart - Javascript Project for Beginners

JavaScript Shopping Cart - javascript shopping cart tutorial for beginnersBuy me a coffee 🍺 https://www.paypal.com/paypalme/ziddahSource Code: https://bit....

The essential JavaScript concepts that you should understand

The essential JavaScript concepts that you should understand - For successful developing and to pass a work interview

Data Types In JavaScript

JavaScript data types are kept easy. While JavaScript data types are mostly similar to other programming languages; some of its data types can be unique. Here, we’ll outline the data types of JavaScript.

Introduction With Basic JavaScript

Introduction With Basic JavaScript - Unlike most programming languages, the JavaScript language has no concept of input or output. It is designed to run as a scripting language in a host environment, and it is up to the host environment to provide mechanisms for communicating with the outside world.