Search in an almost sorted array
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: 2
Explanation: Output is index of 40 in given array i.e. 2Input: arr[] = [10, 3, 40, 20, 50, 80, 70], target = 90
Output: -1
Explanation: 90 is not present in the array.
Table of Content
[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:
#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;
}
import java.util.*;
class GfG {
// Linear search function that returns the
// index of target if found, otherwise -1
public static int findTarget(int[] arr, int target) {
for (int i = 0; i < arr.length; i++) {
if (arr[i] == target)
return i;
}
return -1;
}
public static void main(String[] args) {
int[] arr = { 10, 3, 40, 20, 50, 80, 70 };
int target = 40;
System.out.print(findTarget(arr, target));
}
}
# Linear search function that returns the
# index of target if found, otherwise -1
def findTarget(arr, target):
for i in range(len(arr)):
if arr[i] == target:
return i
return -1
arr = [ 10, 3, 40, 20, 50, 80, 70 ]
target = 40
print(findTarget(arr, target))
using System;
using System.Collections.Generic;
class GfG {
// Linear search function that returns the
// index of target if found, otherwise -1
public static int findTarget(int[] arr, int target) {
for (int i = 0; i < arr.Length; i++) {
if (arr[i] == target)
return i;
}
return -1;
}
public static void Main() {
int[] arr = { 10, 3, 40, 20, 50, 80, 70 };
int target = 40;
Console.Write(findTarget(arr, target));
}
}
// Linear search function that returns the
// index of target if found, otherwise -1
function findTarget(arr, target) {
for (let i = 0; i < arr.length; i++) {
if (arr[i] === target)
return i;
}
return -1;
}
let arr = [ 10, 3, 40, 20, 50, 80, 70 ];
let target = 40;
console.log(findTarget(arr, target));
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:
#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;
}
import java.util.*;
class GfG {
// Bianry search function that returns the
// index of target if found, otherwise -1
public static int findTarget(int[] arr, int target) {
int l = 0, r = arr.length - 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;
}
public static void main(String[] args) {
int[] arr = { 10, 3, 40, 20, 50, 80, 70 };
int target = 40;
System.out.print(findTarget(arr, target));
}
}
# Bianry search function that returns the
# index of target if found, otherwise -1
def findTarget(arr, target):
l, r = 0, len(arr) - 1
while r >= l:
mid = l + (r - l) // 2
# Check the middle 3 positions
if arr[mid] == target:
return mid
if mid > l and arr[mid - 1] == target:
return mid - 1
if mid < r and 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
arr = [ 10, 3, 40, 20, 50, 80, 70 ]
target = 40
print(findTarget(arr, target))
using System;
using System.Collections.Generic;
class GfG {
// Bianry search function that returns the
// index of target if found, otherwise -1
public static int findTarget(int[] arr, int target) {
int l = 0, r = arr.Length - 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;
}
public static void Main() {
int[] arr = { 10, 3, 40, 20, 50, 80, 70 };
int target = 40;
Console.Write(findTarget(arr, target));
}
}
// Bianry search function that returns the
// index of target if found, otherwise -1
function findTarget(arr, target) {
let l = 0, r = arr.length - 1;
while (r >= l) {
let mid = l + Math.floor((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;
}
let arr = [ 10, 3, 40, 20, 50, 80, 70 ];
let target = 40;
console.log(findTarget(arr, target));
Output
2