📜  从自然数中删除一些整数后的第K个最小元素

📅  最后修改于: 2021-04-23 16:02:42             🧑  作者: Mango

给定大小为‘n’的数组arr []和正整数k 。考虑一系列自然数,并从中删除arr [0],arr [1],arr [2],…,arr [p]。现在的任务是在剩余的自然数集中找到第k个最小的数。如果不存在这样的数字,则打印“ -1”。

例子 :

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, ... }.

方法1(简单):
为存在/不存在自然数创建一个辅助数组b [],并用0初始化所有整数。使数组arr []中存在的所有等于1的整数,即b [arr [i]] =1。现在,运行a每当遇到未标记的单元格时,循环并递减k。当k的值为0时,我们得到答案。

以下是此方法的实现:

C++
// C++ program to find the K-th smallest element
// after removing some integers from natural number.
#include 
#define MAX 1000000
using namespace std;
 
// Return the K-th smallest element.
int ksmallest(int arr[], int n, int k)
{
    // Making an array, and mark all number as unmarked.
    int b[MAX];
    memset(b, 0, sizeof b);
 
    // Marking the number present in the given array.
    for (int i = 0; i < n; i++)
        b[arr[i]] = 1;
 
    for (int j = 1; j < MAX; j++) {
        // If j is unmarked, reduce k by 1.
        if (b[j] != 1)
            k--;
 
        // If k is 0 return j.
        if (!k)
            return j;
    }
}
 
// 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 {
 
    static final int MAX = 1000000;
 
    // Return the K-th smallest element.
    static int ksmallest(int arr[], int n, int k)
    {
        // Making an array, and mark
        // all number as unmarked.
        int b[] = new int[MAX];
 
        // Marking the number present
        // in the given array.
        for (int i = 0; i < n; i++) {
            b[arr[i]] = 1;
        }
 
        for (int j = 1; j < MAX; j++) {
            // If j is unmarked, reduce k by 1.
            if (b[j] != 1) {
                k--;
            }
 
            // If k is 0 return j.
            if (k != 1) {
                return j;
            }
        }
        return Integer.MAX_VALUE;
    }
 
    // Driven 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 has been contributed by 29AjayKumar


Python3
# Python program to find the K-th smallest element
# after removing some integers from natural number.
MAX = 1000000
 
 
# Return the K-th smallest element.
def ksmallest(arr, n, k):
     
    # Making an array, and mark all number as unmarked.
    b = [0]*MAX;
 
    # Marking the number present in the given array.
    for i in range(n):
        b[arr[i]] = 1;
 
    for j in range(1, MAX):
        # If j is unmarked, reduce k by 1.
        if (b[j] != 1):
            k-= 1;
 
        # If k is 0 return j.
        if (k is not 1):
            return j;
             
# Driven Program
k = 1;
arr = [ 1 ];
n = len(arr);
print(ksmallest(arr, n, k));
 
# This code contributed by Rajput-Ji


C#
// C# program to find the K-th smallest element
// after removing some integers from natural number.
using System;
 
class GFG {
 
    static int MAX = 1000000;
 
    // Return the K-th smallest element.
    static int ksmallest(int[] arr, int n, int k)
    {
        // Making an array, and mark
        // all number as unmarked.
        int[] b = new int[MAX];
 
        // Marking the number present
        // in the given array.
        for (int i = 0; i < n; i++) {
            b[arr[i]] = 1;
        }
 
        for (int j = 1; j < MAX; j++) {
            // If j is unmarked, reduce k by 1.
            if (b[j] != 1) {
                k--;
            }
 
            // If k is 0 return j.
            if (k != 1) {
                return j;
            }
        }
        return int.MaxValue;
    }
 
    // Driven code
    public static void Main()
    {
        int k = 1;
        int[] arr = { 1 };
        int n = arr.Length;
        Console.WriteLine(ksmallest(arr, n, k));
    }
}
 
/* This code contributed by PrinciRaj1992 */


PHP


Javascript


C++
// C++ program to find the Kth smallest element
// after removing some integer from first n
// natural number.
#include 
using namespace std;
 
// Return the K-th smallest element.
int ksmallest(int arr[], int n, int k)
{
    sort(arr, arr + n);
 
    // Checking if k lies before 1st element
    if (k < arr[0])
        return k;
 
    // If k is the first element of array arr[].
    if (k == arr[0])
        return arr[0] + 1;
 
    // If k is more than last element
    if (k > arr[n - 1])
        return k + n;
 
    // If first element of array is 1.
    if (arr[0] == 1)
        k--;
 
    // Reducing k by numbers before arr[0].
    else
        k -= (arr[0] - 1);
 
    // Finding k'th smallest element after removing
    // array elements.
    for (int i = 1; i < n; i++) {
        // Finding count of element between i-th
        // and (i-1)-th element.
        int c = arr[i] - arr[i - 1] - 1;
        if (k <= c)
            return arr[i - 1] + k;
        else
            k -= c;
    }
 
    return arr[n - 1] + 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
// Kth smallest element after
// removing some integer from
// first n natural number.
import java.util.Arrays;
import java.io.*;
 
class GFG {
 
    // Return the K-th
    // smallest element.
    static int ksmallest(int arr[],
                         int n, int k)
    {
        // sort(arr, arr+n);
        Arrays.sort(arr);
 
        // Checking if k lies
        // before 1st element
        if (k < arr[0])
            return k;
 
        // If k is the first
        // element of array arr[].
        if (k == arr[0])
            return arr[0] + 1;
 
        // If k is more
        // than last element
        if (k > arr[n - 1])
            return k + n;
 
        // If first element
        // of array is 1.
        if (arr[0] == 1)
            k--;
 
        // Reducing k by numbers
        // before arr[0].
        else
            k -= (arr[0] - 1);
 
        // Finding k'th smallest
        // element after removing
        // array elements.
        for (int i = 1; i < n; i++) {
            // Finding count of
            // element between i-th
            // and (i-1)-th element.
            int c = arr[i] - arr[i - 1] - 1;
            if (k <= c)
                return arr[i - 1] + k;
            else
                k -= c;
        }
 
        return arr[n - 1] + k;
    }
 
    // Driven 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 ajit


Python3
# Python3 program to find the Kth
# smallest element after
# removing some integer from
# first n natural number.
 
# Return the K-th
# smallest element.
def ksmallest(arr, n, k):
 
    arr.sort();
 
    # Checking if k lies
    # before 1st element
    if (k < arr[0]):
        return k;
 
    # If k is the first
    # element of array arr[].
    if (k == arr[0]):
        return arr[0] + 1;
 
    # If k is more
    # than last element
    if (k > arr[n - 1]):
        return k + n;
 
    # If first element
    # of array is 1.
    if (arr[0] == 1):
        k-= 1;
 
    # Reducing k by numbers
    # before arr[0].
    else:
        k -= (arr[0] - 1);
 
    # Finding k'th smallest element
    # after removing array elements.
    for i in range(1, n):
        # Finding count of element between
        # i-th and (i-1)-th element.
        c = arr[i] - arr[i - 1] - 1;
        if (k <= c):
            return arr[i - 1] + k;
        else:
            k -= c;
 
    return arr[n - 1] + 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
// Kth smallest element after
// removing some integer from
// first n natural number.
using System;
 
class GFG {
    // Return the K-th
    // smallest element.
    static int ksmallest(int[] arr,
                         int n, int k)
    {
        // sort(arr, arr+n);
        Array.Sort(arr);
 
        // Checking if k lies
        // before 1st element
        if (k < arr[0])
            return k;
 
        // If k is the first
        // element of array arr[].
        if (k == arr[0])
            return arr[0] + 1;
 
        // If k is more
        // than last element
        if (k > arr[n - 1])
            return k + n;
 
        // If first element
        // of array is 1.
        if (arr[0] == 1)
            k--;
 
        // Reducing k by numbers
        // before arr[0].
        else
            k -= (arr[0] - 1);
 
        // Finding k'th smallest
        // element after removing
        // array elements.
        for (int i = 1; i < n; i++) {
            // Finding count of
            // element between i-th
            // and (i-1)-th element.
            int c = arr[i] - arr[i - 1] - 1;
            if (k <= c)
                return arr[i - 1] + k;
            else
                k -= c;
        }
 
        return arr[n - 1] + 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
// by ajit


PHP
 $arr[$n - 1])
        return $k + $n;
 
    // If first element
    // of array is 1.
    if ($arr[0] == 1)
        $k--;
 
    // Reducing k by numbers
    // before arr[0].
    else
        $k -= ($arr[0] - 1);
 
    // Finding k'th smallest element
    // after removing array elements.
    for ($i = 1; $i < $n; $i++)
    {
        // Finding count of element between
        // i-th and (i-1)-th element.
        $c = $arr[$i] - $arr[$i - 1] - 1;
        if ($k <= $c)
            return $arr[$i - 1] + $k;
        else
            $k -= $c;
    }
 
    return $arr[$n - 1] + $k;
}
 
// Driver Code
$k = 1;
$arr = array ( 1 );
$n = sizeof($arr);
echo ksmallest($arr, $n, $k);
 
// This code is contributed by aj_36
?>


输出 :

2

时间复杂度: O(n)。方法2(高效):
首先,对数组arr []进行排序。观察到,在0和arr [0]之间将存在arr [0] – 1个数字,类似地,在arr [0]和arr [1]之间将存在arr [1] – arr [0] – 1个数字,依此类推。因此,如果k在arr [i] – arr [i + 1] – 1之间,则返回范围中第K个最小的元素。否则将k减少arr [i] – arr [i + 1] – 1,即k = k –(arr [i] – arr [i + 1] – 1)。

解决问题的算法:

1. Sort the array arr[].
2. For i = 1 to k. Find c = arr[i+1] - arr[i] -1.
  a) if k - c <= 0, return arr[i-1] + k.
  b) else k = k - c.

以下是此方法的实现:

C++

// C++ program to find the Kth smallest element
// after removing some integer from first n
// natural number.
#include 
using namespace std;
 
// Return the K-th smallest element.
int ksmallest(int arr[], int n, int k)
{
    sort(arr, arr + n);
 
    // Checking if k lies before 1st element
    if (k < arr[0])
        return k;
 
    // If k is the first element of array arr[].
    if (k == arr[0])
        return arr[0] + 1;
 
    // If k is more than last element
    if (k > arr[n - 1])
        return k + n;
 
    // If first element of array is 1.
    if (arr[0] == 1)
        k--;
 
    // Reducing k by numbers before arr[0].
    else
        k -= (arr[0] - 1);
 
    // Finding k'th smallest element after removing
    // array elements.
    for (int i = 1; i < n; i++) {
        // Finding count of element between i-th
        // and (i-1)-th element.
        int c = arr[i] - arr[i - 1] - 1;
        if (k <= c)
            return arr[i - 1] + k;
        else
            k -= c;
    }
 
    return arr[n - 1] + 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
// Kth smallest element after
// removing some integer from
// first n natural number.
import java.util.Arrays;
import java.io.*;
 
class GFG {
 
    // Return the K-th
    // smallest element.
    static int ksmallest(int arr[],
                         int n, int k)
    {
        // sort(arr, arr+n);
        Arrays.sort(arr);
 
        // Checking if k lies
        // before 1st element
        if (k < arr[0])
            return k;
 
        // If k is the first
        // element of array arr[].
        if (k == arr[0])
            return arr[0] + 1;
 
        // If k is more
        // than last element
        if (k > arr[n - 1])
            return k + n;
 
        // If first element
        // of array is 1.
        if (arr[0] == 1)
            k--;
 
        // Reducing k by numbers
        // before arr[0].
        else
            k -= (arr[0] - 1);
 
        // Finding k'th smallest
        // element after removing
        // array elements.
        for (int i = 1; i < n; i++) {
            // Finding count of
            // element between i-th
            // and (i-1)-th element.
            int c = arr[i] - arr[i - 1] - 1;
            if (k <= c)
                return arr[i - 1] + k;
            else
                k -= c;
        }
 
        return arr[n - 1] + k;
    }
 
    // Driven 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 ajit

Python3

# Python3 program to find the Kth
# smallest element after
# removing some integer from
# first n natural number.
 
# Return the K-th
# smallest element.
def ksmallest(arr, n, k):
 
    arr.sort();
 
    # Checking if k lies
    # before 1st element
    if (k < arr[0]):
        return k;
 
    # If k is the first
    # element of array arr[].
    if (k == arr[0]):
        return arr[0] + 1;
 
    # If k is more
    # than last element
    if (k > arr[n - 1]):
        return k + n;
 
    # If first element
    # of array is 1.
    if (arr[0] == 1):
        k-= 1;
 
    # Reducing k by numbers
    # before arr[0].
    else:
        k -= (arr[0] - 1);
 
    # Finding k'th smallest element
    # after removing array elements.
    for i in range(1, n):
        # Finding count of element between
        # i-th and (i-1)-th element.
        c = arr[i] - arr[i - 1] - 1;
        if (k <= c):
            return arr[i - 1] + k;
        else:
            k -= c;
 
    return arr[n - 1] + 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
// Kth smallest element after
// removing some integer from
// first n natural number.
using System;
 
class GFG {
    // Return the K-th
    // smallest element.
    static int ksmallest(int[] arr,
                         int n, int k)
    {
        // sort(arr, arr+n);
        Array.Sort(arr);
 
        // Checking if k lies
        // before 1st element
        if (k < arr[0])
            return k;
 
        // If k is the first
        // element of array arr[].
        if (k == arr[0])
            return arr[0] + 1;
 
        // If k is more
        // than last element
        if (k > arr[n - 1])
            return k + n;
 
        // If first element
        // of array is 1.
        if (arr[0] == 1)
            k--;
 
        // Reducing k by numbers
        // before arr[0].
        else
            k -= (arr[0] - 1);
 
        // Finding k'th smallest
        // element after removing
        // array elements.
        for (int i = 1; i < n; i++) {
            // Finding count of
            // element between i-th
            // and (i-1)-th element.
            int c = arr[i] - arr[i - 1] - 1;
            if (k <= c)
                return arr[i - 1] + k;
            else
                k -= c;
        }
 
        return arr[n - 1] + 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
// by ajit

的PHP

 $arr[$n - 1])
        return $k + $n;
 
    // If first element
    // of array is 1.
    if ($arr[0] == 1)
        $k--;
 
    // Reducing k by numbers
    // before arr[0].
    else
        $k -= ($arr[0] - 1);
 
    // Finding k'th smallest element
    // after removing array elements.
    for ($i = 1; $i < $n; $i++)
    {
        // Finding count of element between
        // i-th and (i-1)-th element.
        $c = $arr[$i] - $arr[$i - 1] - 1;
        if ($k <= $c)
            return $arr[$i - 1] + $k;
        else
            $k -= $c;
    }
 
    return $arr[$n - 1] + $k;
}
 
// Driver Code
$k = 1;
$arr = array ( 1 );
$n = sizeof($arr);
echo ksmallest($arr, $n, $k);
 
// This code is contributed by aj_36
?>

输出 :

2