Finding the nth missing number from an array JavaScript


In the problem mentioned our aim is to find the nth missing number in the given array and implement its solution in Javascript. To solve this question we will use a for loop and some basic functions of Javascript.

Logic for the given problem

Our task is to find the nth number which is missing in the array. For solving this problem we will define two variables to know the position of the missing number and previous element.

So traverse through the input array and check for the missing items between the current and previous items. If the missing number exists between these two items then increment the count of missing items. If the count reaches the given number, the function will calculate and return the missing number in the array.

Algorithm

Step 1 − Define a function which will take two arguments − an array and the value of n. This function will perform the necessary operations to find and return the nth missing number.

Step 2 − Need to keep track of the current and previous values, start initial value to zero for both the variables.

Step 3 − Traverse the items of the defined array with the help of a for loop. Inside this loop check the difference of the array numbers and previous numbers. If it is bigger than 1 then increase the count with the difference of both the numbers.

Step 4 − Check if the count value is greater than or equal to the specified value of n. If the condition is true then calculate and return the missing value by subtracting n from the current value and add it to the previous number in the array.

Step 5 − As we want to show the missing nth number.

Code for the algorithm

//function to get the required item
function findMissingNumer(numArr, n) {
   let missingCount = 0;
   let prevNum = 0;
    
   for (let i = 0; i < numArr.length; i++) {
      const currNum = numArr[i];
      if (currNum - prevNum > 1) {
         missingCount += currNum - prevNum - 1;
         if (missingCount >= n) {
            const diff = n - (missingCount - currNum + prevNum + 1);
            return prevNum + diff;
         }
      }
      prevNum = currNum;
   }
   return prevNum + n - missingCount;
}
const arr = [1, 4, 6, 8, 10];
const n = 4;
const result = findMissingNumer(arr, n);
console.log(result);

Time and space Complexity

The function of the above code is running for n times so the time complexity is O(n), because every step of the loop is running for a constant amount of time to get the required number in the array. And the space consumed by the algorithm is O(1), as we are only storing one variable to store the missing number and not any additional data structure or array.

Conclusion

The code we've seen to find the nth missing number from an array utilizes fundamental JavaScript operations to accomplish the task. The algorithm has a linear time complexity, which is considered an efficient solution for finding the nth number in an array.

Updated on: 18-May-2023

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements