Missing Numbers: Javascript Coding Challenge

Problem Statement: 


Youtube Video: https://youtu.be/RQnMZXyxX9M


You are given a list of whole numbers in ascending order. You need to find which numbers are missing in the sequence.

Create a program that takes in a list of numbers and outputs the missing numbers in the sequence separated by spaces.

The first input denotes the length of the list (N). The next N lines contain the list elements as integers.

Output: A string containing a space-separated list of the missing numbers.

Sample Input

5

2

4

5

7

8

Sample Output

3 6



Solution: 


// Get input from user using stdin

process.stdin.setEncoding('utf-8');

console.log('Enter the numbers separated by ,');

process.stdin.on('data', (data) => {

    // First we will convert the number numbers into an array

    const givenNumbers = data.toString().split(',').map(Number);

    // Here split will separate the numbers by , and create an array

    // .map will convert modify the array and (Number) will convert the string numbers into int numbers

    const max = Number(givenNumbers[givenNumbers.length - 1]);

    const min = Number(givenNumbers[0]);

    // Next we will create a sequential array from min to max

    const sequentialArray = Array(max-min+1).fill(0).map((_, i) => min + i);

    // After creating the sequential array we will compare both arrays

    console.log('Missing numbers are: ', sequentialArray.filter(x => !givenNumbers.includes(x)));

})





Comments

Popular Posts