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++

`// 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 Program ` `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; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// 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 code ` `public` `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. ` |

*chevron_right*

*filter_none*

## Python3

`# 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 searched ` `def` `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 ` `=` `[` `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. ` |

*chevron_right*

*filter_none*

## C#

`// 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 code ` `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)); ` `} ` `} ` ` ` `// This code is contributed by vt_m. ` |

*chevron_right*

*filter_none*

## PHP

`<?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. ` `?> ` |

*chevron_right*

*filter_none*

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.

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.

## Recommended Posts:

- Search an element in an array where difference between adjacent elements is 1
- Interpolation search vs Binary search
- Why is Binary Search preferred over Ternary Search?
- Sublist Search (Search a linked list in another list)
- Best First Search (Informed Search)
- Linear Search vs Binary Search
- Repeatedly search an element by doubling it after every successful search
- Meta Binary Search | One-Sided Binary Search
- Minimum absolute difference of adjacent elements in a circular array
- Maximum adjacent difference in an array in its sorted form
- Minimize the maximum difference of adjacent elements after at most K insertions
- Construct a sequence from given frequencies of N consecutive integers with unit adjacent difference
- Searching in an array where adjacent differ by at most k
- Distinct adjacent elements in a binary array
- Minimum decrements required such that sum of all adjacent pairs in an Array does not exceed K
- Search an element in a sorted and rotated array
- Search in an almost sorted array
- Search in an array of strings where non-empty strings are sorted
- Search, insert and delete in an unsorted array
- Search, insert and delete in a sorted array