Sparse Search
Last Updated :
03 Aug, 2022
Given a sorted array of strings which is interspersed with empty strings, write a method to find the location of a given string.
Examples:
Input : arr[] = {"for", "geeks", "", "", "", "", "ide",
"practice", "", "", "", "quiz"}
x = "geeks"
Output : 1
Input : arr[] = {"for", "geeks", "", "", "", "", "ide",
"practice", "", "", "", "quiz"},
x = "ds"
Output : -1
If there were no empty strings then we could’ve simply performed binary search. We can still use Binary Search with a little modification. If our mid is empty we just have to move mid to closest Non-Empty string.
Below is a implementation of above approach.
C++
#include <bits/stdc++.h>
using namespace std;
int binarySearch(string *arr, int low, int high, string x) {
if (low > high)
return -1;
int mid = (low + high) / 2;
if (arr[mid] == "") {
int left = mid - 1;
int right = mid + 1;
while (1) {
if (left < low && right > high)
return -1;
if (left >= low && arr[left] != "") {
mid = left;
break;
}
else if (right <= high && arr[right] != "") {
mid = right;
break;
}
left--;
right++;
}
}
if (arr[mid] == x)
return mid;
else if (arr[mid] > x)
return binarySearch(arr, low, mid - 1, x);
else
return binarySearch(arr, mid + 1, high, x);
}
int sparseSearch(string arr[], string x, int n) {
return binarySearch(arr, 0, n - 1, x);
}
int main() {
ios_base::sync_with_stdio(false);
string arr[] = {"for", "geeks", "", "", "", "", "ide",
"practice", "", "", "", "quiz"};
string x = "geeks";
int n = sizeof(arr) / sizeof(arr[0]);
int index = sparseSearch(arr, x, n);
if (index != -1)
cout << x << " found at index " << index << "\n";
else
cout << x << " not found\n";
return 0;
}
|
Java
class solution
{
static int binarySearch(String arr[], int low, int high, String x) {
if (low > high)
return -1;
int mid = (low + high) / 2;
if (arr[mid] == "") {
int left = mid - 1;
int right = mid + 1;
while (true) {
if (left < low && right > high)
return -1;
if (left >= low && arr[left] != "") {
mid = left;
break;
}
else if (right <= high && arr[right] != "") {
mid = right;
break;
}
left--;
right++;
}
}
if (arr[mid] == x)
return mid;
else if (x.compareTo(arr[mid]) < 0)
return binarySearch(arr, low, mid - 1, x);
else
return binarySearch(arr, mid + 1, high, x);
}
static int sparseSearch(String arr[], String x, int n) {
return binarySearch(arr, 0, n - 1, x);
}
public static void main(String args[]) {
String arr[] = {"for", "geeks", "", "", "", "", "ide",
"practice", "", "", "", "quiz"};
String x = "geeks";
int n = x.length();
int index = sparseSearch(arr, x, n);
if (index != -1)
System.out.println(x+ " found at index "+index);
else
System.out.println(x+" not found");
}
}
|
Python3
def sparseSearch(arr , key , low , high):
left = 0; right = 0
while low <= high:
mid = low + (high - low) // 2
if arr[mid] == '':
left = mid - 1
right = mid + 1
while True:
if left < low and right > high:
return -1
elif left >= low and arr[left] != '':
mid = left
break
elif right <= high and arr[right] != '':
mid = right
break
left -= 1
right += 1
if arr[mid] == key:
print('Found string {} at index {}'.format
(arr[mid] , mid))
return mid
elif arr[mid] > key:
high = mid - 1
elif arr[mid] < key:
low = mid + 1
return -1
if __name__ == '__main__':
arr = ["for", "geeks", "", "", "", "", "ide",
"practice", "", "", "", "quiz"]
key = 'geeks'
low = 0
high = len(arr) - 1
sparseSearch(arr , key , low , high)
|
C#
using System;
using System.Collections.Generic;
class GFG{
static int binarySearch(string []arr, int low,
int high, string x)
{
if (low > high)
return -1;
int mid = (low + high) / 2;
if (arr[mid] == "")
{
int left = mid - 1;
int right = mid + 1;
while (true)
{
if (left < low && right > high)
return -1;
if (left >= low && arr[left] != "")
{
mid = left;
break;
}
else if (right <= high &&
arr[right] != "")
{
mid = right;
break;
}
left--;
right++;
}
}
if (arr[mid] == x)
return mid;
else if (x.CompareTo(arr[mid]) < 0)
return binarySearch(arr, low,
mid - 1, x);
else
return binarySearch(arr, mid + 1,
high, x);
}
static int sparseSearch(string []arr,
string x, int n)
{
return binarySearch(arr, 0, n - 1, x);
}
public static void Main(string[] args)
{
string []arr = { "for", "geeks", "", "",
"", "", "ide", "practice",
"", "", "", "quiz"};
string x = "geeks";
int n = x.Length;
int index = sparseSearch(arr, x, n);
if (index != -1)
Console.Write(x + " found at index " +
index);
else
Console.Write(x + " not found");
}
}
|
Javascript
<script>
function binarySearch(arr, low, high, x) {
if (low > high)
return -1;
let mid = Math.floor((low + high) / 2);
if (arr[mid] == "")
{
let left = mid - 1;
let right = mid + 1;
while (true) {
if (left < low && right > high)
return -1;
if (left >= low && arr[left] != "") {
mid = left;
break;
}
else if (right <= high && arr[right] != "") {
mid = right;
break;
}
left--;
right++;
}
}
if (arr[mid] == x)
return mid;
else if (arr[mid] > x)
return binarySearch(arr, low, mid - 1, x);
else
return binarySearch(arr, mid + 1, high, x);
}
function sparseSearch(arr, x, n) {
return binarySearch(arr, 0, n - 1, x);
}
let arr = ["for", "geeks", "", "", "", "", "ide",
"practice", "", "", "", "quiz"];
let x = "geeks";
let n = x.length;
let index = sparseSearch(arr, x, n);
if (index != -1)
document.write(x + " found at index " + index);
else
document.write(x + " not found");
</script>
|
Output:
geeks found at index 1
Time Complexity: O(logn)
Auxiliary Space: O(logn)
Share your thoughts in the comments
Please Login to comment...