Efficient search in an array where difference between adjacent is 1
Given an array of n integers. Each array element is obtained by adding either +1 or -1 to previous element i.e absolute difference between any two consecutive elements is 1. The task is to search an element index with the minimum number of comparison (less than simple element by element search). If the element is present multiple time, then print the smallest index. If the element is not present print -1.
Examples:
Input : arr[] = {5, 4, 5, 6, 5, 4, 3, 2}
x = 4.
Output : 1
The first occurrence of element x is at
index 1.
Input : arr[] = { 5, 4, 5, 6, 4, 3, 2, 3 }
x = 9.
Output : -1
Element x is not present in arr[]
Let element to be search is x. At any index i, if arr[i] != x, the possibility of x to be present is at location i + abs(arr[i] – a), since each element is obtained by adding either +1 or -1 to the previous element. There is no possibility of having el between i and i + abs(arr[i] – a). So directly jump to i + abs(arr[i] – a), if arr[i] != x.
Algorithm to solve the problem:
1. Start from index = 0.
2. Compare arr[index] and x.
a) If both are equal, return index.
b) If not, set index = index + abs(arr[index] - x).
3. Repeat step 2.
Below is the implementation of the above idea :
C++
#include<bits/stdc++.h>
using namespace std;
int search( int arr[], int n, int x)
{
int i = 0;
while (i <= n-1)
{
if (arr[i] == x)
return i;
i += abs (arr[i]-x);
}
return -1;
}
int main()
{
int arr[] = {5, 4, 5, 6, 5, 4, 3, 2};
int n = sizeof (arr)/ sizeof (arr[0]);
int x = 4;
cout << search(arr, n, x) << endl;
return 0;
}
|
Java
import java.io.*;
class GFG
{
static int search( int arr[], int n, int x)
{
int i = 0 ;
while (i <= n- 1 )
{
if (arr[i] == x)
return i;
i += Math.abs(arr[i]-x);
}
return - 1 ;
}
public static void main (String[] args)
{
int arr[] = { 5 , 4 , 5 , 6 , 5 , 4 , 3 , 2 };
int n = arr.length;
int x = 6 ;
System.out.println(search(arr, n, x));
}
}
|
Python3
def search(arr, n, x):
i = 0
while (i < = n - 1 ):
if (arr[i] = = x):
return i
i + = abs (arr[i] - x)
return - 1
arr = [ 5 , 4 , 5 , 6 , 5 , 4 , 3 , 2 ]
n = len (arr)
x = 4
print (search(arr, n, x))
|
C#
using System;
class GFG
{
static int search( int []arr, int n,
int x)
{
int i = 0;
while (i <= n - 1)
{
if (arr[i] == x)
return i;
i += Math.Abs(arr[i] - x);
}
return -1;
}
public static void Main ()
{
int []arr = {5, 4, 5, 6, 5, 4, 3, 2};
int n = arr.Length;
int x = 4;
Console.WriteLine(search(arr, n, x));
}
}
|
PHP
<?php
function search( $arr , $n , $x )
{
$i = 0;
while ( $i <= $n -1)
{
if ( $arr [ $i ] == $x )
return $i ;
$i += abs ( $arr [ $i ] - $x );
}
return -1;
}
$arr = array (5, 4, 5, 6, 5, 4, 3, 2);
$n = sizeof( $arr );
$x = 4;
echo search( $arr , $n , $x ) ;
?>
|
Javascript
<script>
function search(arr, n, x)
{
let i = 0;
while (i <= n-1)
{
if (arr[i] == x)
return i;
i += Math.abs(arr[i]-x);
}
return -1;
}
let arr = [5, 4, 5, 6, 5, 4, 3, 2];
let n = arr.length;
let x = 4;
document.write(search(arr, n, x));
</script>
|
Time Complexity: O(n), where n is the size of the given array
Auxiliary Space: O(1), as no extra space is required
Last Updated :
11 Sep, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...