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

Given an array arr[] of size ‘n’ and a positive integer k. Consider series of natural numbers and remove arr, arr, arr, …, 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, ... }.
```

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

## C++

 `// C++ program to find the K-th smallest element ` `// after removing some integers from natural number. ` `#include ` `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); ` `    ``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 `

## Python3

 `# 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 `

## PHP

 ` `

Output:

```2
```

My Personal Notes arrow_drop_up Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

Improved By : jit_t, andrew1234, Mithun Kumar

Article Tags :
Practice Tags :

Be the First to upvote.

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.