Skip to content
Related Articles

Related Articles

Improve Article

Lower Insertion Point

  • Last Updated : 16 Mar, 2021

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:
 

 

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++




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

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




<?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.
?>

Javascript




<script>
 
    // Javascript 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;
       
        let lowerPnt = 0;
        let 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;
    
     
    let arr = [ 2, 3, 4, 4, 5, 6, 7, 9 ];
    let n = arr.length;
    let X = 4;
    document.write(LowerInsertionPoint(arr, n, X));
     
</script>
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.
 

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.




My Personal Notes arrow_drop_up
Recommended Articles
Page :