📜  下插入点

📅  最后修改于: 2021-04-26 09:54:28             🧑  作者: Mango

给定一个由n个排序的整数元素和一个整数X组成的数组arr [] ,任务是在数组中找到X的下插入点。下插入点是≥X的第一个元素的索引。如果X大于arr的所有元素,则输出n ;如果X小于arr []的所有元素,则返回0
例子:

方法:

  • 如果X 打印0X> arr [n – 1]打印n
  • 初始化lowertPnt = 0并开始从1遍历到n – 1
    • 如果arr [i] 则更新lowerPnt = ii = i * 2
    • i的第一个值,其中X≥ARR [I]或当I≥N,中断环路的进行。
    • 现在检查其余元素,从lowerPntn – 1 ,而arr [lowerPnt] 更新lowerPnt = lowerPnt + 1。
    • 最后打印lowerPnt

下面是上述方法的实现:

C++
// C++ program to find the lower insertion point
// of an element in a sorted array
#include 
using namespace std;
 
// Function to return the lower insertion point
// of an element in a sorted array
int LowerInsertionPoint(int arr[], int n, int X)
{
 
    // Base cases
    if (X < arr[0])
        return 0;
    else if (X > arr[n - 1])
        return n;
 
    int lowerPnt = 0;
    int i = 1;
 
    while (i < n && arr[i] < X) {
        lowerPnt = i;
        i = i * 2;
    }
 
    // Final check for the remaining elements which are < X
    while (lowerPnt < n && arr[lowerPnt] < X)
        lowerPnt++;
 
    return lowerPnt;
}
 
// Driver code
int main()
{
    int arr[] = { 2, 3, 4, 4, 5, 6, 7, 9 };
    int n = sizeof(arr) / sizeof(arr[0]);
    int X = 4;
    cout << LowerInsertionPoint(arr, n, X);
    return 0;
}


Java
//Java program to find the lower insertion point
//of an element in a sorted array
public class AQES {
 
    //Function to return the lower insertion point
    //of an element in a sorted array
    static int LowerInsertionPoint(int arr[], int n, int X)
    {
 
     // Base cases
     if (X < arr[0])
         return 0;
     else if (X > arr[n - 1])
         return n;
 
     int lowerPnt = 0;
     int i = 1;
 
     while (i < n && arr[i] < X) {
         lowerPnt = i;
         i = i * 2;
     }
 
     // Final check for the remaining elements which are < X
     while (lowerPnt < n && arr[lowerPnt] < X)
         lowerPnt++;
 
     return lowerPnt;
    }
 
    //Driver code
    public static void main(String[] args) {
         
         int arr[] = { 2, 3, 4, 4, 5, 6, 7, 9 };
         int n = arr.length;
         int X = 4;
         System.out.println(LowerInsertionPoint(arr, n, X));
 
    }
}


Python3
# Python3 program to find the lower insertion
# point of an element in a sorted array
 
# Function to return the lower insertion
# point of an element in a sorted array
def LowerInsertionPoint(arr, n, X) :
 
    # Base cases
    if (X < arr[0]) :
        return 0;
    elif (X > arr[n - 1]) :
        return n
 
    lowerPnt = 0
    i = 1
 
    while (i < n and arr[i] < X) :
        lowerPnt = i
        i = i * 2
 
    # Final check for the remaining elements
    # which are < X
    while (lowerPnt < n and arr[lowerPnt] < X) :
        lowerPnt += 1
 
    return lowerPnt
 
# Driver code
if __name__ == "__main__" :
 
    arr = [ 2, 3, 4, 4, 5, 6, 7, 9 ]
    n = len(arr)
    X = 4
    print(LowerInsertionPoint(arr, n, X))
 
# This code is contributed by Ryuga


C#
// C#  program to find the lower insertion point
//of an element in a sorted array
using System;
 
public class GFG{
    //Function to return the lower insertion point
    //of an element in a sorted array
    static int LowerInsertionPoint(int []arr, int n, int X)
    {
 
    // Base cases
    if (X < arr[0])
        return 0;
    else if (X > arr[n - 1])
        return n;
 
    int lowerPnt = 0;
    int i = 1;
 
    while (i < n && arr[i] < X) {
        lowerPnt = i;
        i = i * 2;
    }
 
    // Final check for the remaining elements which are < X
    while (lowerPnt < n && arr[lowerPnt] < X)
        lowerPnt++;
 
    return lowerPnt;
    }
 
    //Driver code
    static public void Main (){
        int []arr = { 2, 3, 4, 4, 5, 6, 7, 9 };
        int n = arr.Length;
        int X = 4;
        Console.WriteLine(LowerInsertionPoint(arr, n, X));
    }
}


PHP
 $arr[$n - 1])
        return $n;
 
    $lowerPnt = 0;
    $i = 1;
 
    while ($i < $n && $arr[$i] < $X)
    {
        $lowerPnt = $i;
        $i = $i * 2;
    }
 
    // Final check for the remaining
    // elements which are < X
    while ($lowerPnt < $n && $arr[$lowerPnt] < $X)
        $lowerPnt++;
 
    return $lowerPnt;
}
 
// Driver code
$arr = array( 2, 3, 4, 4, 5, 6, 7, 9 );
$n = sizeof($arr);
$X = 4;
echo LowerInsertionPoint($arr, $n, $X);
 
// This code is contributed by ajit.
?>


Javascript


输出:
2

进一步优化:在最坏的情况下,上述解决方案的时间复杂度可能变为O(n)。我们可以使用Binary Search优化解决方案使其在O(Log n)时间内工作。请参考无界二进制搜索以获取详细信息。