# 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

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

Approach:

• If X < arr 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 ` `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) ` `        ``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); ` `    ``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) ` `        ``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. ` `?> `

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.

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.