Open In App

Search in an almost sorted array

Last Updated : 05 May, 2025
Comments
Improve
Suggest changes
106 Likes
Like
Report

Given a sorted integer array arr[] consisting of distinct elements, where some elements of the array are moved to either of the adjacent positions, i.e. arr[i] may be present at arr[i-1] or arr[i+1].
Given an integer target.  You have to return the index ( 0-based ) of the target in the array. If target is not present return -1.

Examples : 

Input: arr[] =  [10, 3, 40, 20, 50, 80, 70], target = 40
Output:
Explanation: Output is index of 40 in given array i.e. 2

Input: arr[] =  [10, 3, 40, 20, 50, 80, 70], target = 90
Output: -1
Explanation: 90 is not present in the array.

[Naive Approach] - Linear Search - O(n) Time and O(1) Space

A idea is to linearly search the given key in array arr[]. To do so, run a loop starting from index 0 to n - 1, and for each index i, check if arr[i] == target, if so print the index i, else move to the next index. At last, if no such index is found, print -1.

Below is given the implementation:

C++
#include <bits/stdc++.h>
using namespace std;

// Linear search function that returns the 
// index of target if found, otherwise -1
int findTarget(vector<int>& arr, int target) {
    for (int i = 0; i < arr.size(); i++) {
        if (arr[i] == target) 
            return i; 
    }
    return -1; 
}

int main() {
    vector<int> arr = { 10, 3, 40, 20, 50, 80, 70 };
    int target = 40;
    cout << findTarget(arr, target);
    return 0;
}
Java Python C# JavaScript

Output
2

[Expected Approach] - Using Binary Search - O(log n) Time and O(1) Space

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.

Follow the steps below to implement the idea:

  • Initialize a variable mid with l+(r-l)/2.
  • If arr[mid] is equal to x return mid 
  • Else if arr[mid-1] is equal to x return mid-1 
  • Else if arr[mid+1] is equal to x return mid+1
  • If arr[mid] > x recur for search space r to mid-2 else recur for search space mid+2 to l.

Below is given the implementation:

C++
#include <bits/stdc++.h>
using namespace std;

// Bianry search function that returns the 
// index of target if found, otherwise -1
int findTarget(vector<int>& arr, int target) {
    int l = 0, r = arr.size() - 1;

    while (r >= l) {
        int mid = l + (r - l) / 2;

        // Check the middle 3 positions
        if (arr[mid] == target) 
            return mid;
        if (mid > l && arr[mid - 1] == target) 
            return mid - 1;
        if (mid < r && arr[mid + 1] == target) 
            return mid + 1;

        // Search in left subarray
        if (arr[mid] > target) 
            r = mid - 2;

        // Search in right subarray
        else 
            l = mid + 2;
    }

    // Element not found
    return -1; 
}

int main() {
    vector<int> arr = { 10, 3, 40, 20, 50, 80, 70 };
    int target = 40;
    cout << findTarget(arr, target);
    return 0;
}
Java Python C# JavaScript

Output
2

Article Tags :
Practice Tags :

Similar Reads