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: 2Input: 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++
// 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. ?> |
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.
Recommended Posts:
- Lower and Upper Bound Theory
- Binary Insertion Sort
- Optimal sequence for AVL tree insertion (without any rotations)
- Minimum number of operations to move all uppercase characters before all lower case characters
- Mid-Point Circle Drawing Algorithm
- Best meeting point in 2D binary array
- Mid-Point Line Generation Algorithm
- Find the transition point in a binary array
- Point Clipping Algorithm in Computer Graphics
- Find a partition point in array to maximize its xor sum
- Draw a circle without floating point arithmetic
- Find a Fixed Point (Value equal to index) in a given array
- Find the number of points that have atleast 1 point above, below, left or right of it
- Find a Fixed Point in an array with duplicates allowed
- Binary Search for Rational Numbers without using floating point arithmetic
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.



