Given an array which is sorted, but after sorting some elements are moved to either of the adjacent positions, i.e., arr[i] may be present at arr[i+1] or arr[i-1]. Write an efficient function to search an element in this array. Basically the element arr[i] can only be swapped with either arr[i+1] or arr[i-1].

For example consider the array {2, 3, 10, 4, 40}, 4 is moved to next position and 10 is moved to previous position.

Example:

Input: arr[] =  {10, 3, 40, 20, 50, 80, 70}, key = 40
Output: 2 
Output is index of 40 in given array

Input: arr[] =  {10, 3, 40, 20, 50, 80, 70}, key = 90
Output: -1
-1 is returned to indicate element is not present

A simple solution is to linearly search the given key in given array. Time complexity of this solution is O(n). We cab modify binary search to do it in O(Logn) time.

[ad type=”banner”]

The idea is to compare the key with middle 3 elements, if present then return the index. If not present, then compare the key with middle element to decide whether to go in left half or right half. Comparing with middle element is enough as all the elements after mid+2 must be greater than element mid and all elements before mid-2 must be smaller than mid element.

Following is C++ implementation of this approach.

C++

[pastacode lang=”cpp” manual=”%2F%2F%20C%2B%2B%20program%20to%20find%20an%20element%20in%20an%20almost%20sorted%0A%2F%2F%20array%0A%23include%20%3Cstdio.h%3E%0A%20%0A%2F%2F%20A%20recursive%20binary%20search%20based%20function.%20It%20returns%0A%2F%2F%20index%20of%20x%20in%20given%20array%20arr%5Bl..r%5D%20is%20present%2C%20%0A%2F%2F%20otherwise%20-1%0Aint%20binarySearch(int%20arr%5B%5D%2C%20int%20l%2C%20int%20r%2C%20int%20x)%0A%7B%0A%20%20%20if%20(r%20%3E%3D%20l)%0A%20%20%20%7B%0A%20%20%20%20%20%20%20%20int%20mid%20%3D%20l%20%2B%20(r%20-%20l)%2F2%3B%0A%20%0A%20%20%20%20%20%20%20%20%2F%2F%20If%20the%20element%20is%20present%20at%20one%20of%20the%20middle%20%0A%20%20%20%20%20%20%20%20%2F%2F%203%20positions%0A%20%20%20%20%20%20%20%20if%20(arr%5Bmid%5D%20%3D%3D%20x)%20%20return%20mid%3B%0A%20%20%20%20%20%20%20%20if%20(mid%20%3E%20l%20%26%26%20arr%5Bmid-1%5D%20%3D%3D%20x)%20return%20(mid%20-%201)%3B%0A%20%20%20%20%20%20%20%20if%20(mid%20%3C%20r%20%26%26%20arr%5Bmid%2B1%5D%20%3D%3D%20x)%20return%20(mid%20%2B%201)%3B%0A%20%0A%20%20%20%20%20%20%20%20%2F%2F%20If%20element%20is%20smaller%20than%20mid%2C%20then%20it%20can%20only%20%0A%20%20%20%20%20%20%20%20%2F%2F%20be%20present%20in%20left%20subarray%0A%20%20%20%20%20%20%20%20if%20(arr%5Bmid%5D%20%3E%20x)%20return%20binarySearch(arr%2C%20l%2C%20mid-2%2C%20x)%3B%0A%20%0A%20%20%20%20%20%20%20%20%2F%2F%20Else%20the%20element%20can%20only%20be%20present%20in%20right%20subarray%0A%20%20%20%20%20%20%20%20return%20binarySearch(arr%2C%20mid%2B2%2C%20r%2C%20x)%3B%0A%20%20%20%7D%0A%20%0A%20%20%20%2F%2F%20We%20reach%20here%20when%20element%20is%20not%20present%20in%20array%0A%20%20%20return%20-1%3B%0A%7D%0A%20%0A%2F%2F%20Driver%20program%20to%20test%20above%20function%0Aint%20main(void)%0A%7B%0A%20%20%20int%20arr%5B%5D%20%3D%20%7B3%2C%202%2C%2010%2C%204%2C%2040%7D%3B%0A%20%20%20int%20n%20%3D%20sizeof(arr)%2F%20sizeof(arr%5B0%5D)%3B%0A%20%20%20int%20x%20%3D%204%3B%0A%20%20%20int%20result%20%3D%20binarySearch(arr%2C%200%2C%20n-1%2C%20x)%3B%0A%20%20%20(result%20%3D%3D%20-1)%3F%20printf(%22Element%20is%20not%20present%20in%20array%22)%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%3A%20printf(%22Element%20is%20present%20at%20index%20%25d%22%2C%20result)%3B%0A%20%20%20return%200%3B%0A%7D%0A” message=”c++” highlight=”” provider=”manual”/]

JAVA

[pastacode lang=”java” manual=”%2F%2F%20Java%20program%20to%20find%20an%20element%20in%20an%20almost%20sorted%20array%0Aclass%20SearchAlmost%0A%7B%0A%20%20%20%20%2F%2F%20A%20recursive%20binary%20search%20based%20function.%20It%20returns%0A%20%20%20%20%2F%2F%20index%20of%20x%20in%20given%20array%20arr%5Bl..r%5D%20is%20present%2C%0A%20%20%20%20%2F%2F%20otherwise%20-1%0A%20%20%20%20int%20binarySearch(int%20arr%5B%5D%2C%20int%20l%2C%20int%20r%2C%20int%20x)%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20if%20(r%20%3E%3D%20l)%0A%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20int%20mid%20%3D%20l%20%2B%20(r%20-%20l)%2F2%3B%0A%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%2F%2F%20If%20the%20element%20is%20present%20at%20one%20of%20the%20middle%0A%20%20%20%20%20%20%20%20%20%20%20%20%2F%2F%203%20positions%0A%20%20%20%20%20%20%20%20%20%20%20%20if%20(arr%5Bmid%5D%20%3D%3D%20x)%20%20return%20mid%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20if%20(mid%20%3E%20l%20%26%26%20arr%5Bmid-1%5D%20%3D%3D%20x)%20return%20(mid%20-%201)%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20if%20(mid%20%3C%20r%20%26%26%20arr%5Bmid%2B1%5D%20%3D%3D%20x)%20return%20(mid%20%2B%201)%3B%0A%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%2F%2F%20If%20element%20is%20smaller%20than%20mid%2C%20then%20it%20can%0A%20%20%20%20%20%20%20%20%20%20%20%20%2F%2F%20only%20be%20present%20in%20left%20subarray%0A%20%20%20%20%20%20%20%20%20%20%20%20if%20(arr%5Bmid%5D%20%3E%20x)%20return%20binarySearch(arr%2C%20l%2C%20mid-2%2C%20x)%3B%0A%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%2F%2F%20Else%20the%20element%20can%20only%20be%20present%20in%20right%0A%20%20%20%20%20%20%20%20%20%20%20%20%2F%2F%20subarray%0A%20%20%20%20%20%20%20%20%20%20%20%20return%20binarySearch(arr%2C%20mid%2B2%2C%20r%2C%20x)%3B%0A%20%20%20%20%20%20%20%20%7D%0A%20%0A%20%20%20%20%20%20%20%20%2F%2F%20We%20reach%20here%20when%20element%20is%20not%20present%20in%20array%0A%20%20%20%20%20%20%20%20return%20-1%3B%0A%20%20%20%20%7D%0A%20%0A%20%20%20%20%2F%2F%20Driver%20method%0A%20%20%20%20public%20static%20void%20main(String%20args%5B%5D)%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20abc%20ob%20%3D%20new%20abc()%3B%0A%20%20%20%20%20%20%20%20int%20arr%5B%5D%20%3D%20%7B3%2C%202%2C%2010%2C%204%2C%2040%7D%3B%0A%20%20%20%20%20%20%20%20int%20n%20%3D%20arr.length%3B%0A%20%20%20%20%20%20%20%20int%20x%20%3D%204%3B%0A%20%20%20%20%20%20%20%20int%20result%20%3D%20ob.binarySearch(arr%2C%200%2C%20n-1%2C%20x)%3B%0A%20%20%20%20%20%20%20%20if(result%20%3D%3D%20-1)%0A%20%20%20%20%20%20%20%20%20%20%20%20System.out.println(%22Element%20is%20not%20present%20in%20array%22)%3B%0A%20%20%20%20%20%20%20%20else%0A%20%20%20%20%20%20%20%20%20%20%20%20System.out.println(%22Element%20is%20present%20at%20index%20%22%20%2B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20result)%3B%0A%20%20%20%20%7D%0A%7D” message=”JAVA” highlight=”” provider=”manual”/]

Output:

Element is present at index 3

Time complexity of the above function is O(Logn).

[ad type=”banner”]