Lower Insertion Point
Last Updated :
11 Oct, 2022
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++
#include <iostream>
using namespace std;
int LowerInsertionPoint( int arr[], int n, int X)
{
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;
}
while (lowerPnt < n && arr[lowerPnt] < X)
lowerPnt++;
return lowerPnt;
}
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
public class AQES {
static int LowerInsertionPoint( int arr[], int n, int X)
{
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 ;
}
while (lowerPnt < n && arr[lowerPnt] < X)
lowerPnt++;
return lowerPnt;
}
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
def LowerInsertionPoint(arr, n, X) :
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
while (lowerPnt < n and arr[lowerPnt] < X) :
lowerPnt + = 1
return lowerPnt
if __name__ = = "__main__" :
arr = [ 2 , 3 , 4 , 4 , 5 , 6 , 7 , 9 ]
n = len (arr)
X = 4
print (LowerInsertionPoint(arr, n, X))
|
C#
using System;
public class GFG{
static int LowerInsertionPoint( int []arr, int n, int X)
{
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;
}
while (lowerPnt < n && arr[lowerPnt] < X)
lowerPnt++;
return lowerPnt;
}
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
function LowerInsertionPoint( $arr , $n , $X )
{
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;
}
while ( $lowerPnt < $n && $arr [ $lowerPnt ] < $X )
$lowerPnt ++;
return $lowerPnt ;
}
$arr = array ( 2, 3, 4, 4, 5, 6, 7, 9 );
$n = sizeof( $arr );
$X = 4;
echo LowerInsertionPoint( $arr , $n , $X );
?>
|
Javascript
<script>
function LowerInsertionPoint(arr, n, X)
{
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;
}
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>
|
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.
Auxiliary space:O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...