K-th Smallest Element after Removing some Integers from Natural Numbers using JavaScript
Last Updated :
17 May, 2024
Given an array of size "n" and a positive integer k our task is to find the K-th smallest element that remains after removing certain integers from the set of natural numbers using JavaScript.
Example:
Input: array = [3, 5]
k = 2
Output: 2
Explanation: The natural numbers are [1, 2, 3, 4, 5......] and after removing elements 3 and 5 from the natural numbers, the remaining numbers are [1, 2, 4....].
The 2nd smallest number from the remaining numbers is 2.
Below are the approaches to finding the K-th smallest element after removing some integers from natural numbers using JavaScript:
Brute Force approach
In this approach, Generate all the natural numbers up to a certain limit, then filter out the numbers that are not present in the given array. After filtering, sort the remaining numbers and return the K-th smallest one.
Example: The example below shows how to find the K-th smallest element after removing some integers from natural numbers using JavaScript.
JavaScript
function kthSmallestAfterRemoval(arr, k, MAX) {
const naturalNumbers =
Array.from({ length: MAX }, (_, i) => i + 1);
const remainingNumbers =
naturalNumbers.filter(num => !arr.includes(num));
remainingNumbers.sort((a, b) => a - b);
return remainingNumbers[k - 1];
}
const arr = [3, 5];
const k = 2;
const MAX = 10;
console.log("K-th smallest element after removal:",
kthSmallestAfterRemoval(arr, k, MAX));
OutputK-th smallest element after removal: 2
Time complexity: O(MAX + n log n)
Space complexity: O(MAX + n)
Using Quick Select
In this approach, Implement the Quick Select algorithm to find the K-th smallest element in an array. Modify the algorithm to handle the removal of integers that do not meet the specified condition. Use the modified Quick Select algorithm to find the K-th smallest integer after removing the specified integers from the natural numbers
Example: The example below shows how to find the K-th smallest element after removing some integers from natural numbers using Quick Select.
JavaScript
function partition(arr, left, right, pivotIndex) {
const pivotValue = arr[pivotIndex];
let storeIndex = left;
// Move pivot to the end
[arr[pivotIndex], arr[right]] =
[arr[right], arr[pivotIndex]];
// Partition the array
for (let i = left; i < right; i++) {
if (arr[i] < pivotValue) {
[arr[i], arr[storeIndex]] =
[arr[storeIndex], arr[i]];
storeIndex++;
}
}
// Move pivot to its final place
[arr[right], arr[storeIndex]] =
[arr[storeIndex], arr[right]];
return storeIndex;
}
function quickSelect(arr, left, right, k) {
while (left <= right) {
const pivotIndex = Math.floor(Math.random() *
(right - left + 1)) + left;
const partitionIndex =
partition(arr, left, right, pivotIndex);
if (partitionIndex === k) {
return arr[partitionIndex];
} else if (partitionIndex < k) {
left = partitionIndex + 1;
} else {
right = partitionIndex - 1;
}
}
}
function kthSmallestAfterRemoval(arr, k, MAX) {
const remainingNumbers = [];
let num = 1;
while (remainingNumbers.length < k) {
if (!arr.includes(num)) {
remainingNumbers.push(num);
}
num++;
}
return quickSelect(remainingNumbers, 0,
remainingNumbers.length - 1, k - 1);
}
const arr = [3, 5];
const k = 2;
const MAX = 10;
console.log("K-th smallest element after removal:",
kthSmallestAfterRemoval(arr, k, MAX));
OutputK-th smallest element after removal: 2
Time complexity: O(n)
Space complexity: O(k)
Flag and Counting Approach
In this approach, Create an array flag of size MAX and initialize all elements to 0. Iterate through the array arr. For each element array[i], set flag[array[i]] to 1 to mark its presence Iterate from 1 to MAX. For each value i, if flag[i] is not equal to 1 indicating that i is not present in the array arr, decrement k. If k becomes 0, return the current value of i as the k-th smallest number. If no k-th smallest element is found return 0.
Example: The example below shows how to find the K-th smallest element after removing some integers from natural numbers using the Flag and Counting Approach.
JavaScript
const MAX = 1000000;
function smallestNumber(arr, n, k) {
const flag = new Array(MAX).fill(0);
for (let i = 0; i < n; i++) {
flag[arr[i]] = 1;
}
for (let i = 1; i < MAX; i++) {
if (flag[i] !== 1) {
k--;
}
if (k === 0) {
return i;
}
}
return 0;
}
const k = 2;
const arr = [3, 5];
console.log(smallestNumber(arr, 2, k));
Time Complexity: O(MAX)
Space Complexity: O(MAX)
Using Sets
In this approach, Initialize an empty set to store the integers in the array. Start a loop from 1 (the smallest natural number). Check if the current number exists in the set. If it doesn't, increment the count of found smallest elements. Inside the loop, if the count of found smallest elements equals k, return the current number.
Example: The example below shows how to find K-th smallest element after removing some integers from natural numbers using Sets.
JavaScript
function kthSmallestAfterRemoval(array, k) {
// Create a Set to store integers in the array
const set = new Set(array);
// Count smallest elements
let count = 0;
for (let i = 1; ; i++) {
// increment the count
if (!set.has(i)) {
count++;
// Return the k-th smallest number
if (count === k) return i;
}
}
}
const array = [3, 5];
const k = 2;
console.log(kthSmallestAfterRemoval(array, k));
Time Complexity: O(n + k).
Space Complexity: O(n)
Similar Reads
How to Remove Smallest and Largest Elements from an Array in JavaScript ?
We will cover how to remove the smallest and largest elements from an array in JavaScript. Removing the smallest and largest elements from an array in JavaScript means filtering out the minimum and maximum values from the original array, leaving only the intermediate elements. We will see the code f
5 min read
JavaScript Program to Find Smallest Number that is Divisible by First n Numbers
Given a number, our task is to find the smallest number that is evenly divisible by the first n numbers without giving any remainder in JavaScript. Example: Input: 10Output: 2520 Explanation: 2520 is smallest number which is completely divisible by numbers from 1 to 10. Below are the following appro
4 min read
JavaScript Program to Find the Smallest Among Three Numbers
In JavaScript, finding the smallest among three numbers is a common task in programming. There are multiple approaches to achieving this, each with its advantages and use cases. There are several approaches in JavaScript to find the smallest among three numbers which are as follows: Table of Content
2 min read
First Element to Occur K Times using JavaScript
Given an array and number, our task is to find the first element that occurs exactly k times in an array using JavaScript. Example: Input: array= [1, 2, 3, 4, 2, 5, 6, 4, 2] and k = 3.Output: 2Explanation: Here the first element that occurred 3 times in the array is 2Table of Content Using nested fo
3 min read
Remove Least Elements to Convert Array to Increasing Sequence using JavaScript
Given an array of numbers, our task is to find the minimum number of elements that need to be removed from the array so that the remaining elements form an increasing sequence. Examples: Input: arr = [5, 100, 6, 7, 100, 9, 8];Output: No of elements removed =3, Array after removing elements =[ 5, 6 ,
3 min read
JavaScript Program to Find the Largest Three Elements in an Array
In this article, we are given an array of numbers, we need to find the largest three elements in an array in JavaScript. We will explore the easiest and most efficient code of each approach and also go through the output of the code. Approaches to find the largest three elements in an array in JavaS
5 min read
Generate N Random Numbers Between a Range & Pick the Greatest in JavaScript
Given n random numbers between a range, our task is to pick the greatest number and print it using JavaScript. Example: Input: arr[] = {4, 6, 8, -5, -4, 4} Output: 8Explanation: Maximum number is 8Table of Content Using a loopUsing Array methods Using reduce MethodUsing a loopIn this approach, we us
2 min read
JavaScript - Find Second Largest Element in an Array
Here are the different approaches to find the second largest element in an arary using JavaScript. 1. Using SortingSort the array in ascending order. Iterate from the end of the sorted array to find the first element that is different from the largest. If no such element is found, return null, indic
4 min read
JavaScript Program to Find k Most Frequent Elements in Array
In this article, we are given an input array with the elements and k value. Our task is to find out the most frequent elements in the array as per the k value using JavaScript. Below we have added the example for better understanding. Example: Input: array = [7, 10, 11, 5, 2, 5, 5, 7, 11, 8, 9] , K
3 min read
JavaScript Program to Find the Factors of a Number
We have given the input number and we need to find all the factors of that number. Factors of the number are nothing but the numbers that divide the original input number evenly with the remainder of 0. Below we have added the example for better understanding: Example:Input: n = 10Output: 1 2 5 10In
3 min read