Efficient search in an array where difference between adjacent is 1 - GeeksforGeeks

26 Mar, 2021
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 above idea :  
C++
Java
Python3
C#
PHP
Javascript
// C++ program to search an element in an array// where each element is obtained by adding// either +1 or -1 to previous element.#include<bits/stdc++.h>using namespace std; // Return the index of the element to be searched.int search(int arr[], int n, int x){    // Searching x in arr[0..n-1]    int i = 0;    while (i <= n-1)    {        // Checking if element is found.        if (arr[i] == x)            return i;         // Else jumping to abs(arr[i]-x).        i += abs(arr[i]-x);    }     return -1;} // Driven Programint 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 program to search an element// in an array where each element is// obtained by adding either +1 or// -1 to previous element.class GFG{     // Return the index of the// element to be searched.static int search(int arr[], int n, int x){    // Searching x in arr[0..n-1]    int i = 0;    while (i <= n-1)    {        // Checking if element is found.        if (arr[i] == x)            return i;         // Else jumping to abs(arr[i]-x).        i += Math.abs(arr[i]-x);    }     return -1;} // Driver codepublic static void main (String[] args){    int arr[] = {5, 4, 5, 6, 5, 4, 3, 2};    int n = arr.length;    int x = 4;     System.out.println(search(arr, n, x));}} // This code is contributed by Anant Agarwal.
# Python program to search an element in# an array where each element is obtained# by adding either +1 or -1 to previous element # Return the index of the element to be searcheddef search(arr, n, x):     # Searching x in arr[0..n-1]    i = 0    while (i <= n-1):             # Checking if element is found.        if (arr[i] == x):            return i         # Else jumping to abs(arr[i]-x).        i += abs(arr[i] - x)         return -1 # Driver codearr = [5, 4, 5, 6, 5, 4, 3, 2]n = len(arr)x = 4 print(search(arr, n, x)) # This code is contributed by Anant Agarwal.
// C# program to search an element in// an array where each element is// obtained by adding either + 1 or// -1 to previous element.using System; class GFG{     // Return the index of the// element to be searched.static int search(int []arr, int n,                  int x){         // Searching x in arr[0.. n - 1]    int i = 0;    while (i <= n - 1)    {        // Checking if element is found        if (arr[i] == x)            return i;         // Else jumping to abs(arr[i] - x)        i += Math.Abs(arr[i] - x);    }     return -1;} // Driver codepublic 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));}} // This code is contributed by vt_m.
<?php// PHP program to search an// element in an array where// each element is obtained// by adding either +1 or -1// to previous element. // Return the index of the// element to be searched.function search($arr, $n, $x){         // Searching x in arr[0..n-1]    $i = 0;    while ($i <= $n-1)    {                 // Checking if element        // is found.        if ($arr[$i] == $x)            return $i;         // Else jumping to        // abs(arr[i]-x).        $i += abs($arr[$i] - $x);    }     return -1;}     // Driver Code    $arr= array(5, 4, 5, 6, 5, 4, 3, 2);    $n = sizeof($arr);    $x = 4;     echo search($arr, $n, $x) ; // This code is contributed by nitin mittal.?>
<script> // JavaScript program to search an element// in an array where each element is// obtained by adding either +1 or// -1 to previous element.  // Return the index of the// element to be searched.function search(arr, n, x){    // Searching x in arr[0..n-1]    let i = 0;    while (i <= n-1)    {        // Checking if element is found.        if (arr[i] == x)            return i;           // Else jumping to abs(arr[i]-x).        i += Math.abs(arr[i]-x);    }       return -1;} // Driver code    let arr = [5, 4, 5, 6, 5, 4, 3, 2];    let n = arr.length;    let x = 4;       document.write(search(arr, n, x)); </script>
Output: 
1
This article is contributed by Anuj Chauhan. 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 write comments if you find anything incorrect, or you want to share more information about the topic discussed above. 
vt_m
nitin mittal
chinmoy1997pal
Searching
Searching
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Search an element in a sorted and rotated array
Program to find largest element in an array
k largest(or smallest) elements in an array
Given an array of size n and a number k, find all elements that appear more than n/k times
Median of two sorted arrays of different sizes
Find the index of an array element in Java
Two Pointers Technique
Most frequent element in an array
Best First Search (Informed Search)
Find the smallest and second smallest elements in an array