Lower Insertion Point

Given an array arr[] of n sorted integer elements and an integer X, the task is to find the lower insertion point of X in the array. The lower insertion point is the index of the first element that is ≥ X. If X is greater than all the elements of arr then print n and if X is less than all the elements of arr[] then return 0.

Examples:

Input: arr[] = {2, 3, 4, 4, 5, 6, 7, 9}, X = 4
Output: 2

Input: arr[] = {0, 5, 8, 15}, X = 16
Output: 4

Approach:

  • If X < arr[0] print 0 or X > arr[n – 1] print n.
  • Initialise lowertPnt = 0 and start traversing the array from 1 to n – 1.
    • If arr[i] < X then update lowerPnt = i and i = i * 2.
    • The first value of i for which X ≥ arr[i] or when i ≥ n, break out of the loop.
    • Now check for the rest of the elements from lowerPnt to n – 1, while arr[lowerPnt] < X update lowerPnt = lowerPnt + 1.
    • Print lowerPnt in the end..

Below is the implementation of the above approach:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to find the lower insertion point
// of an element in a sorted array
#include <iostream>
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;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

//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));
  
    }
}

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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));
    }
}

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// PHP 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
function LowerInsertionPoint($arr, $n, $X)
{
  
    // Base cases
    if ($X < $arr[0])
        return 0;
    else if ($X > $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.
?>

chevron_right


Output:

2


Further Optimization :
The time complexity of the above solution can become O(n) in worst case. We can optimize the solution to work in O(Log n) time using Binary Search. Please refer unbounded binary search for details.



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, AnkitRai01, Ita_c, Sach_Code