📌  相关文章
📜  在给定的N个范围内生成的序列中找到第k个元素

📅  最后修改于: 2021-05-04 16:32:01             🧑  作者: Mango

给定N个非重叠范围L []R [] ,其中每个范围均在前一个范围结束后开始,即,对于所有有效i, L [i]> R [i – 1] 。任务是找到在所有给定范围内的所有元素按升序排序后形成的序列中的K元素。

例子:

方法:想法是使用二进制搜索。一个数组总计,用于存储直到i索引为止存在的整数数量,现在借助此数组可以找到第k整数所在的索引。假设索引为j ,现在计算L [j]到R [j]区间中第k最小整数的位置,并使用二分查找法找到第k最小整数,其中lowL [j]highR [j]

下面是上述方法的实现:

C++
// C++ implementation of the approach
#include 
using namespace std;
  
// Function to return the kth element
// of the required series
int getKthElement(int n, int k, int L[], int R[])
{
    int l = 1;
    int h = n;
  
    // To store the number of integers that lie
    // upto the ith index
    int total[n + 1];
  
    total[0] = 0;
  
    // Compute the number of integers
    for (int i = 0; i < n; i++) {
        total[i + 1] = total[i] + (R[i] - L[i]) + 1;
    }
  
    // Stores the index, lying from 1
    // to n,
    int index = -1;
  
    // Using binary search, find the index
    // in which the kth element will lie
    while (l <= h) {
        int m = (l + h) / 2;
  
        if (total[m] > k) {
            index = m;
            h = m - 1;
        }
        else if (total[m] < k)
            l = m + 1;
        else {
            index = m;
            break;
        }
    }
  
    l = L[index - 1];
    h = R[index - 1];
  
    // Find the position of the kth element
    // in the interval in which it lies
    int x = k - total[index - 1];
  
    while (l <= h) {
        int m = (l + h) / 2;
  
        if ((m - L[index - 1]) + 1 == x) {
            return m;
        }
  
        else if ((m - L[index - 1]) + 1 > x)
            h = m - 1;
  
        else
            l = m + 1;
    }
}
  
// Driver code
int main()
{
    int L[] = { 1, 8, 21 };
    int R[] = { 4, 10, 23 };
    int n = sizeof(L) / sizeof(int);
  
    int k = 6;
  
    cout << getKthElement(n, k, L, R);
  
    return 0;
}


Java
// Java implementation of the approach
class GFG
{
      
// Function to return the kth element
// of the required series
static int getKthElement(int n, int k, 
                         int L[], int R[])
{
    int l = 1;
    int h = n;
  
    // To store the number of integers that lie
    // upto the ith index
    int total[] = new int[n + 1];
  
    total[0] = 0;
  
    // Compute the number of integers
    for (int i = 0; i < n; i++) 
    {
        total[i + 1] = total[i] + 
                      (R[i] - L[i]) + 1;
    }
  
    // Stores the index, lying from 1
    // to n,
    int index = -1;
  
    // Using binary search, find the index
    // in which the kth element will lie
    while (l <= h) 
    {
        int m = (l + h) / 2;
  
        if (total[m] > k) 
        {
            index = m;
            h = m - 1;
        }
        else if (total[m] < k)
            l = m + 1;
        else 
        {
            index = m;
            break;
        }
    }
  
    l = L[index - 1];
    h = R[index - 1];
  
    // Find the position of the kth element
    // in the interval in which it lies
    int x = k - total[index - 1];
  
    while (l <= h)
    {
        int m = (l + h) / 2;
  
        if ((m - L[index - 1]) + 1 == x) 
        {
            return m;
        }
  
        else if ((m - L[index - 1]) + 1 > x)
            h = m - 1;
  
        else
            l = m + 1;
    }
    return k;
}
  
// Driver code
public static void main(String[] args)
{
    int L[] = { 1, 8, 21 };
    int R[] = { 4, 10, 23 };
    int n = L.length;
  
    int k = 6;
  
    System.out.println(getKthElement(n, k, L, R));
}
}
  
// This code is contributed by Code_Mech


Python3
# Python3 implementation of the approach
   
# Function to return the kth element
# of the required series
def getKthElement(n, k, L, R):
    l = 1
    h = n
   
    # To store the number of integers that lie
    # upto the ith index
    total=[0 for i in range(n + 1)]
   
    total[0] = 0
   
    # Compute the number of integers
    for i in range(n):
        total[i + 1] = total[i] + (R[i] - L[i]) + 1
   
    # Stores the index, lying from 1
    # to n,
    index = -1
   
    # Using binary search, find the index
    # in which the kth element will lie
    while (l <= h):
        m = (l + h) // 2
   
        if (total[m] > k):
            index = m
            h = m - 1
        elif (total[m] < k):
            l = m + 1
        else :
            index = m
            break
   
    l = L[index - 1]
    h = R[index - 1]
   
    # Find the position of the kth element
    # in the interval in which it lies
    x = k - total[index - 1]
   
    while (l <= h):
        m = (l + h) // 2
   
        if ((m - L[index - 1]) + 1 == x):
            return m
   
        elif ((m - L[index - 1]) + 1 > x):
            h = m - 1
   
        else:
            l = m + 1
  
# Driver code
  
L=[ 1, 8, 21]
R=[4, 10, 23]
n = len(L)
  
k = 6
  
print(getKthElement(n, k, L, R))
  
# This code is contributed by mohit kumar


C#
// C# implementation of the approach
using System;
  
class GFG
{
      
// Function to return the kth element
// of the required series
static int getKthElement(int n, int k, 
                        int[] L, int[] R)
{
    int l = 1;
    int h = n;
  
    // To store the number of integers that lie
    // upto the ith index
    int[] total = new int[n + 1];
  
    total[0] = 0;
  
    // Compute the number of integers
    for (int i = 0; i < n; i++) 
    {
        total[i + 1] = total[i] + 
                    (R[i] - L[i]) + 1;
    }
  
    // Stores the index, lying from 1
    // to n,
    int index = -1;
  
    // Using binary search, find the index
    // in which the kth element will lie
    while (l <= h) 
    {
        int m = (l + h) / 2;
  
        if (total[m] > k) 
        {
            index = m;
            h = m - 1;
        }
        else if (total[m] < k)
            l = m + 1;
        else
        {
            index = m;
            break;
        }
    }
  
    l = L[index - 1];
    h = R[index - 1];
  
    // Find the position of the kth element
    // in the interval in which it lies
    int x = k - total[index - 1];
  
    while (l <= h)
    {
        int m = (l + h) / 2;
  
        if ((m - L[index - 1]) + 1 == x) 
        {
            return m;
        }
  
        else if ((m - L[index - 1]) + 1 > x)
            h = m - 1;
  
        else
            l = m + 1;
    }
    return k;
}
  
// Driver code
public static void Main()
{
    int[] L = { 1, 8, 21 };
    int[] R = { 4, 10, 23 };
    int n = L.Length;
  
    int k = 6;
  
    Console.WriteLine(getKthElement(n, k, L, R));
}
}
  
// This code is contributed by Code_Mech


PHP
 $k) 
        { 
            $index = $m; 
            $h = $m - 1; 
        } 
        else if ($total[$m] < $k) 
            $l = $m + 1; 
        else 
        { 
            $index = $m; 
            break; 
        } 
    } 
  
    $l = $L[$index - 1]; 
    $h = $R[$index - 1]; 
  
    // Find the position of the kth element 
    // in the interval in which it lies 
    $x = $k - $total[$index - 1]; 
  
    while ($l <= $h) 
    { 
        $m = floor(($l + $h) / 2); 
  
        if (($m - $L[$index - 1]) + 1 == $x) 
        { 
            return $m; 
        } 
  
        else if (($m - $L[$index - 1]) + 1 > $x) 
            $h = $m - 1; 
  
        else
            $l = $m + 1; 
    } 
} 
  
// Driver code 
$L = array( 1, 8, 21 ); 
$R = array( 4, 10, 23 ); 
$n = count($L);
  
$k = 6; 
  
echo getKthElement($n, $k, $L, $R); 
  
// This code is contributed by Ryuga
?>


输出:
9