Open In App

K-th smallest element after removing given integers from natural numbers | Set 2

Last Updated : 08 May, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

Given an array arr[] of size ā€˜nā€™ and a positive integer k. Consider series of natural numbers and remove arr[0], arr[1], arr[2], ā€¦, arr[n-1] from it. Now the task is to find k-th smallest number in the remaining set of natural numbers.

Examples:  

Input : arr[] = { 1 } and k = 1.
Output: 2
Natural numbers are {1, 2, 3, 4, …. }
After removing {1}, we get {2, 3, 4, …}.
Now, K-th smallest element = 2.

Input : arr[] = {1, 3}, k = 4.
Output : 6
First 5 Natural number {1, 2, 3, 4, 5, 6,  .. }
After removing {1, 3}, we get {2, 4, 5, 6, … }.

Two methods are discussed in this article. This post discusses one more method.

Algorithm: 

  1. Start traversing the array, for i ranging from 0 to N-1 
  2. If arr[I] <= k, then increment k. Else break out of traversing the rest of the array 
  3. The obtained k is the final answer  

Note: This algorithm will only work if the input array is sorted. So first sort the given array (if not already) and in this case, the time complexity will become O(N log(N)).

Implementation:

C++
// C++ program to find the K-th smallest element
// after removing some integers from natural number.
#include <bits/stdc++.h>
using namespace std;

// Return the K-th smallest element.
int ksmallest(int arr[], int n, int k)
{
    for (int i = 0; i < n; i++) {
        if (arr[i] <= k)
            k++;
        else
            break;
    }
    return k;
}

// Driven Program
int main()
{
    int k = 1;
    int arr[] = { 1 };
    int n = sizeof(arr) / sizeof(arr[0]);
    cout << ksmallest(arr, n, k);
    return 0;
}
Java
// Java program to find the
// K-th smallest element
// after removing some 
// integers from natural number.
class GFG
{

// Return the K-th 
// smallest element.
static int ksmallest(int arr[], 
                     int n, int k)
{
    for (int i = 0; i < n; i++) 
    {
        if (arr[i] <= k)
            k++;
        else
            break;
    }
    return k;
}

// Driver Code
public static void main(String args[])
{
    int k = 1;
    int arr[] = { 1 };
    int n = arr.length;
    System.out.println(ksmallest(arr, n, k));
}
}

// This code is contributed 
// by Arnab Kundu
Python
# Python3 program to find 
# the K-th smallest element 
# after removing some integers
# from natural number.

# Return the K-th 
# smallest element.
def ksmallest(arr, n, k):
    for i in range(n): 
        if (arr[i] <= k):
            k = k + 1;
        else:
            break;
    return k;

# Driver Code
k = 1;
arr = [1];
n = len(arr);
print(ksmallest(arr, n, k));
    
# This code is contributed by mits
C#
// C# program to find the
// K-th smallest element
// after removing some 
// integers from natural number.
using System;

class GFG
{
// Return the K-th 
// smallest element.

static int ksmallest(int []arr, 
                     int n, int k)
{
    for (int i = 0; i < n; i++) 
    {
        if (arr[i] <= k)
            k++;
        else
            break;
    }
    return k;
}

// Driver Code
static public void Main ()
{
    int k = 1;
    int []arr = { 1 };
    int n = arr.Length;
    Console.WriteLine(ksmallest(arr, 
                                n, k));
}
}

// This code is contributed @ajit
Javascript
    // Javascript program to find the
    // K-th smallest element
    // after removing some 
    // integers from natural number.
    
    // Return the K-th 
    // smallest element.

    function ksmallest(arr, n, k)
    {
        for (let i = 0; i < n; i++) 
        {
            if (arr[i] <= k)
                k++;
            else
                break;
        }
        return k;
    }
    
    let k = 1;
    let arr = [ 1 ];
    let n = arr.length;
   console.log(ksmallest(arr, n, k));

// This code is contributed by suresh07.
PHP
<?php
// PHP program to find the K-th
// smallest element after removing
// some integers from natural number.

// Return the K-th smallest element.
function ksmallest($arr, $n, $k)
{
    for ($i = 0; $i < $n; $i++) 
    {
        if ($arr[$i] <= $k)
            $k++;
        else
            break;
    }
    return $k;
}

    // Driver Code
    $k = 1;
    $arr = array(1);
    $n = sizeof($arr);
    echo ksmallest($arr, $n, $k);
    
// This code is contributed by aj_36
?>

Output
2

Time Complexity: O(N)
Auxiliary Space: O(1)



Similar Reads

K-th smallest element after removing some integers from natural numbers
Given an array arr[] of size 'n' and a positive integer k. Consider series of natural numbers and remove arr[0], arr[1], arr[2], ..., arr[p] from it. Now the task is to find k-th smallest number in the remaining set of natural numbers. If no such number exists print "-1". Examples : Input : arr[] = { 1 } and k = 1.Output: 2Natural numbers are {1, 2
18 min read
Nth natural number after removing all numbers consisting of the digit 9
Given a positive integer N, the task is to find the Nth natural number after removing all the natural numbers containing digit 9. Examples: Input: N = 8Output: 8Explanation:Since 9 is the first natural number that contains the digit 9 and is the 9th natural number, therefore, no removal required to find the 8th natural number, which is 8. Input: N
8 min read
Kth element in permutation of first N natural numbers having all even numbers placed before odd numbers in increasing order
Given two integers N and K, the task is to find the Kth element in the permutation of first N natural numbers arranged such that all the even numbers appear before the odd numbers in increasing order. Examples : Input: N = 10, K = 3 Output: 6Explanation:The required permutation is {2, 4, 6, 8, 10, 1, 3, 5, 7, 9}.The 3rd number in the permutation is
9 min read
Last element of an array after repeatedly removing the first element and appending it to the end of the array twice exactly K times
Given an array arr[] consisting of N integers and a positive integer K, the task is to find the last element present in the array obtained by repeatedly removing the first element of the array and appending it twice to the end of the array K times. Examples: Input: arr[] = {1, 2, 3, 4, 5}, K = 5Output: 5Explanation:Initially, the array is {1, 2, 3,
7 min read
Find the last element after repeatedly removing every second element from either end alternately
Given an array arr[] consisting of N integers, the task is to find the last remaining array element after removing every second element, starting from the first and last alternately. Examples: Input: arr[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}Output: 8Explanation: Elements in the list are: 1 2 3 4 5 6 7 8 9 10Starting from first array element, removing
8 min read
Lexicographically smallest permutation of size A having B integers exceeding all preceding integers
Given two positive integers, A and B, the task is to generate the lexicographically smallest permutation of all integers up to A in which exactly B integers are greater than all its preceding elements. Examples: Input: A = 3, B = 2Output : [1, 3, 2]Explanation:All possible permutations of integers [1, 3] are [(1, 2, 3), (1, 3, 2), (2, 1, 3), (2, 3,
15 min read
Average of remaining elements after removing K largest and K smallest elements from array
Given an array of N integers. The task is to find the average of the numbers after removing k largest elements and k smallest element from the array i.e. calculate the average value of the remaining N - 2K elements. Examples: Input: arr = [1, 2, 4, 4, 5, 6], K = 2Output: 4Remove 2 smallest elements i.e. 1 and 2Remove 2 largest elements i.e. 5 and 6
5 min read
Queries to calculate average of an array after removing K smallest and largest elements with updates
Given two positive integers N and K, initialize an empty array arr[] and Q number of queries of the following two types: addInteger(x): Insert element X in the array arr[]. If the size of the array becomes greater than N, then remove the element from the beginning of the array.calculateSpecialAverage(): Find the average of array elements after remo
19 min read
Finding the Nth term in a sequence formed by removing digit K from natural numbers
Given the integers N, K and an infinite sequence of natural numbers where all the numbers containing the digit K (1&lt;=K&lt;=9) are removed. The task is to return the Nth number of this sequence. Example: Input: N = 12, K = 2Output: 14Explanation: The sequence generated for the above input would be like this: 1, 3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 14
10 min read
Remaining array element after repeated removal of last element and subtraction of each element from next adjacent element
Given an array arr[] consisting of N integers, the task is to find the remaining array element after subtracting each element from its next adjacent element and removing the last array element repeatedly. Examples: Input: arr[] = {3, 4, 2, 1}Output: 4Explanation:Operation 1: The array arr[] modifies to {4 - 3, 2 - 4, 1 - 2} = {1, -2, -1}.Operation
8 min read
Article Tags :
Practice Tags :