Given a sorted array arr[] consisting of N distinct integers and an integer K, the task is to find the index of K, if it’s present in the array arr[]. Otherwise, find the index where K must be inserted to keep the array sorted.
Examples:
Input: arr[] = {1, 3, 5, 6}, K = 5
Output: 2
Explanation: Since 5 is found at index 2 as arr[2] = 5, the output is 2.
Input: arr[] = {1, 3, 5, 6}, K = 2
Output: 1
Explanation: Since 2 is not present in the array but can be inserted at index 1 to make the array sorted.
Naive Approach: Follow the steps below to solve the problem:
- Iterate over every element of the array arr[] and search for integer K.
- If any array element is found to be equal to K, then print index of K.
- Otherwise, if any array element is found to be greater than K, print that index as the insert position of K. If no element is found to be exceeding K, K must be inserted after the last array element.
Below is the implementation of above approach :
C++
#include <bits/stdc++.h>
using namespace std;
int find_index( int arr[], int n, int K)
{
for ( int i = 0; i < n; i++)
if (arr[i] == K)
return i;
else if (arr[i] > K)
return i;
return n;
}
int main()
{
int arr[] = { 1, 3, 5, 6 };
int n = sizeof (arr) / sizeof (arr[0]);
int K = 2;
cout << find_index(arr, n, K) << endl;
return 0;
}
|
C
#include <stdio.h>
int find_index( int arr[], int n, int K)
{
for ( int i = 0; i < n; i++)
if (arr[i] == K)
return i;
else if (arr[i] > K)
return i;
return n;
}
int main()
{
int arr[] = { 1, 3, 5, 6 };
int n = sizeof (arr) / sizeof (arr[0]);
int K = 2;
printf ( "%d\n" , find_index(arr, n, K));
return 0;
}
|
Java
import java.io.*;
class GFG{
static int find_index( int [] arr, int n, int K)
{
for ( int i = 0 ; i < n; i++)
if (arr[i] == K)
return i;
else if (arr[i] > K)
return i;
return n;
}
public static void main(String[] args)
{
int [] arr = { 1 , 3 , 5 , 6 };
int n = arr.length;
int K = 2 ;
System.out.println(find_index(arr, n, K));
}
}
|
Python3
def find_index(arr, n, K):
for i in range (n):
if arr[i] = = K:
return i
elif arr[i] > K:
return i
return n
arr = [ 1 , 3 , 5 , 6 ]
n = len (arr)
K = 2
print (find_index(arr, n, K))
|
C#
using System;
class GFG{
static int find_index( int [] arr, int n, int K)
{
for ( int i = 0; i < n; i++)
if (arr[i] == K)
return i;
else if (arr[i] > K)
return i;
return n;
}
public static void Main()
{
int [] arr = { 1, 3, 5, 6 };
int n = arr.Length;
int K = 2;
Console.WriteLine(find_index(arr, n, K));
}
}
|
Javascript
<script>
function find_index(arr, n, K)
{
for (let i = 0; i < n; i++)
if (arr[i] == K)
return i;
else if (arr[i] > K)
return i;
return n;
}
let arr = [ 1, 3, 5, 6 ];
let n = arr.length;
let K = 2;
document.write(find_index(arr, n, K));
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Efficient Approach: To optimize the above approach, the idea is to use Binary Search. Follow the steps below to solve the problem:
- Set start and end as 0 and N – 1, where the start and end variables denote the lower and upper bound of the search space respectively.
- Calculate mid = (start + end) / 2.
- If arr[mid] is found to be equal to K, print mid as the required answer.
- If arr[mid] exceeds K, set high = mid – 1 Otherwise, set low = mid + 1.
Below is the implementation of above approach :
C++
#include <bits/stdc++.h>
using namespace std;
int find_index( int arr[], int n, int K)
{
int start = 0;
int end = n - 1;
while (start <= end) {
int mid = (start + end) / 2;
if (arr[mid] == K)
return mid;
else if (arr[mid] < K)
start = mid + 1;
else
end = mid - 1;
}
return end + 1;
}
int main()
{
int arr[] = { 1, 3, 5, 6 };
int n = sizeof (arr) / sizeof (arr[0]);
int K = 2;
cout << find_index(arr, n, K) << endl;
return 0;
}
|
C
#include<stdio.h>
int find_index( int arr[], int n, int K)
{
int start = 0;
int end = n - 1;
while (start <= end) {
int mid = (start + end) / 2;
if (arr[mid] == K)
return mid;
else if (arr[mid] < K)
start = mid + 1;
else
end = mid - 1;
}
return end + 1;
}
int main()
{
int arr[] = { 1, 3, 5, 6 };
int n = sizeof (arr) / sizeof (arr[0]);
int K = 2;
printf ( "%d" ,find_index(arr, n, K));
return 0;
}
|
Java
import java.io.*;
class GFG{
static int find_index( int [] arr, int n, int K)
{
int start = 0 ;
int end = n - 1 ;
while (start <= end)
{
int mid = (start + end) / 2 ;
if (arr[mid] == K)
return mid;
else if (arr[mid] < K)
start = mid + 1 ;
else
end = mid - 1 ;
}
return end + 1 ;
}
public static void main(String[] args)
{
int [] arr = { 1 , 3 , 5 , 6 };
int n = arr.length;
int K = 2 ;
System.out.println(find_index(arr, n, K));
}
}
|
Python3
def find_index(arr, n, K):
start = 0
end = n - 1
while start< = end:
mid = (start + end) / / 2
if arr[mid] = = K:
return mid
elif arr[mid] < K:
start = mid + 1
else :
end = mid - 1
return end + 1
arr = [ 1 , 3 , 5 , 6 ]
n = len (arr)
K = 2
print (find_index(arr, n, K))
|
C#
using System;
class GFG{
static int find_index( int [] arr, int n, int K)
{
int start = 0;
int end = n - 1;
while (start <= end)
{
int mid = (start + end) / 2;
if (arr[mid] == K)
return mid;
else if (arr[mid] < K)
start = mid + 1;
else
end = mid - 1;
}
return end + 1;
}
public static void Main()
{
int [] arr = { 1, 3, 5, 6 };
int n = arr.Length;
int K = 2;
Console.WriteLine(find_index(arr, n, K));
}
}
|
Javascript
<script>
function find_index(arr, n, K)
{
let start = 0;
let end = n - 1;
while (start <= end) {
let mid = Math.floor((start + end) / 2);
if (arr[mid] == K)
return mid;
else if (arr[mid] < K)
start = mid + 1;
else
end = mid - 1;
}
return end + 1;
}
let arr = [ 1, 3, 5, 6 ];
let n = arr.length;
let K = 2;
document.write(find_index(arr, n, K) + "<br>" );
</script>
|
Time Complexity: O(log N)
Auxiliary Space: O(1)
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!
Last Updated :
19 Oct, 2023
Like Article
Save Article