Find a String in given Array of Strings using Binary Search
Last Updated :
11 Jul, 2025
Given a sorted array of Strings arr and a string x, The task is to find the index of x in the array using the Binary Search algorithm. If x is not present, return -1.
Examples:
Input: arr[] = {"contribute", "geeks", "ide", "practice"}, x = "ide"
Output: 2
Explanation: The String x is present at index 2.
Input : arr[] = {"contribute", "geeks", "ide", "practice"}, x = "zz"
Output : -1
Explanation: The String "zz" is not present.
The idea is to use Binary Search since the input array is sorted. We maintain two pointers: left and right, and repeatedly check the middle element. If the middle matches the target, we return its index. Otherwise, we narrow the search space by comparing the target with the middle value.
Steps to implement the above idea:
- Initialize two pointers l (left) to 0 and r (right) to the last index of the array.
- Run a while loop while l is less than or equal to r to search within the range.
- Calculate the middle index using the formula: l + (r - l) / 2.
- If the middle element equals the target, return the middle index immediately.
- If the target is greater than the middle element, move l to mid + 1 to search the right half.
- If the target is smaller, move r to mid - 1 to search the left half, else return -1 if not found.
C++
// C++ Code to find a string
// in an array using Binary Search
#include <bits/stdc++.h>
using namespace std;
// Returns index of x if it is present
// in arr, else return -1
int findString(vector<string> &arr, string x) {
int l = 0;
int r = arr.size() - 1;
// Loop to implement Binary Search
while (l <= r) {
// Calculating mid
int m = l + (r - l) / 2;
// Check if x is present at mid
if (arr[m] == x) {
return m;
}
// If x greater, ignore left half
if (arr[m] < x) {
l = m + 1;
}
// If x is smaller, ignore right half
else {
r = m - 1;
}
}
return -1;
}
// Driver code
int main() {
// Input array
vector<string> arr = { "contribute",
"geeks", "ide", "practice" };
// Target element
string x = "ide";
cout << findString(arr, x);
return 0;
}
Java
// Java Code to find a string
// in an array using Binary Search
import java.util.*;
public class GfG {
// Returns index of x if it is present
// in arr, else return -1
public static int findString(String[] arr, String x) {
int l = 0;
int r = arr.length - 1;
// Loop to implement Binary Search
while (l <= r) {
// Calculating mid
int m = l + (r - l) / 2;
// Check if x is present at mid
if (arr[m].equals(x)) {
return m;
}
// If x greater, ignore left half
if (arr[m].compareTo(x) < 0) {
l = m + 1;
}
// If x is smaller, ignore right half
else {
r = m - 1;
}
}
return -1;
}
// Driver code
public static void main(String[] args) {
// Input array
String[] arr = { "contribute", "geeks", "ide", "practice" };
// Target element
String x = "ide";
System.out.println(findString(arr, x));
}
}
Python
# Python Code to find a string
# in an array using Binary Search
# Returns index of x if it is present
# in arr, else return -1
def findString(arr, x):
l = 0
r = len(arr) - 1
# Loop to implement Binary Search
while l <= r:
# Calculating mid
m = l + (r - l) // 2
# Check if x is present at mid
if arr[m] == x:
return m
# If x greater, ignore left half
if arr[m] < x:
l = m + 1
# If x is smaller, ignore right half
else:
r = m - 1
return -1
# Driver code
if __name__ == "__main__":
# Input array
arr = ["contribute", "geeks", "ide", "practice"]
# Target element
x = "ide"
print(findString(arr, x))
C#
// C# Code to find a string
// in an array using Binary Search
using System;
public class GfG {
// Returns index of x if it is present
// in arr, else return -1
public static int findString(string[] arr, string x) {
int l = 0;
int r = arr.Length - 1;
// Loop to implement Binary Search
while (l <= r) {
// Calculating mid
int m = l + (r - l) / 2;
// Check if x is present at mid
if (arr[m] == x) {
return m;
}
// If x greater, ignore left half
if (arr[m].CompareTo(x) < 0) {
l = m + 1;
}
// If x is smaller, ignore right half
else {
r = m - 1;
}
}
return -1;
}
// Driver code
public static void Main() {
// Input array
string[] arr = { "contribute", "geeks", "ide", "practice" };
// Target element
string x = "ide";
Console.WriteLine(findString(arr, x));
}
}
JavaScript
// JavaScript Code to find a string
// in an array using Binary Search
// Returns index of x if it is present
// in arr, else return -1
function findString(arr, x) {
let l = 0;
let r = arr.length - 1;
// Loop to implement Binary Search
while (l <= r) {
// Calculating mid
let m = l + Math.floor((r - l) / 2);
// Check if x is present at mid
if (arr[m] === x) {
return m;
}
// If x greater, ignore left half
if (arr[m] < x) {
l = m + 1;
}
// If x is smaller, ignore right half
else {
r = m - 1;
}
}
return -1;
}
// Driver code
// Input array
let arr = ["contribute", "geeks", "ide", "practice"];
// Target element
let x = "ide";
console.log(findString(arr, x));
Time Complexity: O(m log n) where n is size of the input array and m is the length of the input string x.
Space Complexity: O(1), as only a fixed number of variables used, no extra space.
Explore
DSA Fundamentals
Data Structures
Algorithms
Advanced
Interview Preparation
Practice Problem