Given an array arr[] sorted in decreasing order, and an integer X, the task is to check if X is present in the given array or not. If X is present in the array, print its index ( 0-based indexing). Otherwise, print -1.
Examples:
Input: arr[] = {5, 4, 3, 2, 1}, X = 4
Output: 1
Explanation: Element X (= 4) is present at index 1.
Therefore, the required output is 1.
Input: arr[] = {10, 8, 2, -9}, X = 5
Output: -1
Explanation: Element X (= 5) is not present in the array.
Therefore, the required output is -1.
Naive Approach: The simplest approach to solve the problem is to traverse the array and for each element, check if it is equal to X or not. If any element is found to satisfy that condition, print the index of that element. Otherwise print -1.
Time Complexity: O(N)
Auxiliary Space: O(1)
Efficient Approach: To solve the problem, the idea is to use Binary Search based on the approach discussed in the article search an element in a sorted array. Follow the steps below to solve the problem:
- Compare X with the middle element.
- If X matches with the middle element (arr[mid]), return the index mid.
- If X is found to be greater than the arr[mid], then X can only lie in the subarray [mid + 1, end]. So search for X in the subarray {arr[mid + 1], .., arr[end]} .
- Otherwise, search in the subarray {arr[start], …., arr[mid]}
Below is the implementation of the above approach:
C
#include <stdio.h>
int binarySearch( int arr[], int N, int X)
{
int start = 0;
int end = N;
while (start <= end) {
int mid = start
+ (end - start) / 2;
if (X == arr[mid]) {
return mid;
}
else if (X < arr[mid]) {
start = mid + 1;
}
else {
end = mid - 1;
}
}
return -1;
}
int main()
{
int arr[] = { 5, 4, 3, 2, 1 };
int N = sizeof (arr) / sizeof (arr[0]);
int X = 4;
int res = binarySearch(arr, N, X);
printf ( " %d " , res);
return 0;
}
|
C++
#include <bits/stdc++.h>
using namespace std;
int binarySearch( int arr[], int N, int X)
{
int start = 0;
int end = N;
while (start <= end) {
int mid = start
+ (end - start) / 2;
if (X == arr[mid]) {
return mid;
}
else if (X < arr[mid]) {
start = mid + 1;
}
else {
end = mid - 1;
}
}
return -1;
}
int main()
{
int arr[] = { 5, 4, 3, 2, 1 };
int N = sizeof (arr) / sizeof (arr[0]);
int X = 5;
cout << binarySearch(arr, N, X);
return 0;
}
|
Java
class GFG {
static int binarySearch( int arr[],
int N, int X)
{
int start = 0 ;
int end = N;
while (start <= end) {
int mid = start
+ (end - start) / 2 ;
if (X == arr[mid]) {
return mid;
}
else if (X < arr[mid]) {
start = mid + 1 ;
}
else {
end = mid - 1 ;
}
}
return - 1 ;
}
public static void main(String[] args)
{
int arr[] = { 5 , 4 , 3 , 2 , 1 };
int N = arr.length;
int X = 5 ;
System.out.println(
binarySearch(arr, N, X));
}
}
|
Python3
def binarySearch(arr, N, X):
start = 0
end = N
while (start < = end):
mid = start + (end - start) / / 2
if (X = = arr[mid]):
return mid
elif (X < arr[mid]):
start = mid + 1
else :
end = mid - 1
return - 1
if __name__ = = '__main__' :
arr = [ 5 , 4 , 3 , 2 , 1 ]
N = len (arr)
X = 5
print (binarySearch(arr, N, X))
|
C#
using System;
class GFG{
static int binarySearch( int []arr,
int N, int X)
{
int start = 0;
int end = N;
while (start <= end)
{
int mid = start +
(end - start) / 2;
if (X == arr[mid])
{
return mid;
}
else if (X < arr[mid])
{
start = mid + 1;
}
else
{
end = mid - 1;
}
}
return -1;
}
public static void Main(String[] args)
{
int []arr = {5, 4, 3, 2, 1};
int N = arr.Length;
int X = 5;
Console.WriteLine(binarySearch(arr, N, X));
}
}
|
Javascript
<script>
function binarySearch(arr, N, X)
{
let start = 0;
let end = N;
while (start <= end) {
let mid = Math.floor(start
+ (end - start) / 2);
if (X == arr[mid]) {
return mid;
}
else if (X < arr[mid]) {
start = mid + 1;
}
else {
end = mid - 1;
}
}
return -1;
}
let arr = [ 5, 4, 3, 2, 1 ];
let N = arr.length;
let X = 5;
document.write(binarySearch(arr, N, X));
</script>
|
Time Complexity: O(log2N)
Auxiliary Space: O(1)