Practice
Data Structures and Algorithms
Machine Coding Round (LLD)
System Design & Architecture (HLD)
Frontend UI Machine Coding
Resources
Career Advice and Roadmaps
Data Structures and Algorithms
Machine Coding Round (LLD)
System Design & Architecture (HLD)
Backend Development
Frontend Development
Project Ideas for Software Developers
Core Computer Science
Companies
SDE Jobs & Internships
Interview Questions
Compare Companies
IDE
Online IDE
Collaborative IDE

Search Range Editorial

DSA Editorial, Solution and Code

Practice Problem Link: Search Range | Practice Problem

Please make sure to try solving the problem yourself before looking at the editorial.

Problem Statement

Given a sorted array and a number key, find the index of the first and last occurrence of the key in the array.

If the key is not present return [-1, -1].

Naive Approach

A simple solution is to iterate the array and return the first and last occurrence of the key and if the key is not present return [-1, -1].

Analysis

  • Time Complexity: O(n)
  • Space Complexity: O(1)

Implementation

C++
vector<int> searchRange(vector<int> &arr, int key) {
   int first = -1, last = -1;
   for (int i = 0; i < arr.size(); i++) {
       if (first == - 1 && key == arr[i]) {
           first = i;
       }
       if (key == arr[i]) {
           last = i;
       }
   }
   vector<int> indexes = {first, last};
   return indexes;
}
Java
class Solution {
   int[] searchRange (int[] arr, int key) {
       int first = -1, last = -1;
       for (int i = 0; i < arr.length; i++) {
           if (first == -1 && key == arr[i]) {
               first = i;
           }
           if (key == arr[i]) {
               last = i;
           }
       }
       int[] indexes = {first, last};
       return indexes;
   }
} 

Optimal Approach

Since the array is sorted we can find the first and last indexes of the key using binary search.

As the element just before the first occurrence must be less than the key and the element just after the last occurrence must be greater than the key.

The first and last indexes can be found separately using two different binary search functions.

To find the first occurrence:

  • Initialize four variables high = n-1, low = 0 and mid.
  • Perform a binary search until (high >= low). Assign mid = (high + low) / 2.
  • If arr[mid - 1] is less than key and arr[mid] is equal to key return mid. Otherwise, if arr[mid]  is less than the key, search in the second half otherwise search in the first half.
  • Return -1 as the default condition.

To find the last occurrence:

  • Initialize three variables high = n-1, low = 0 and mid.
  • Perform a binary search until (high >= low). Assign mid = (high + low) / 2.
  • If arr[mid] is equal to key and arr[mid + 1] is greater than the key return mid. Otherwise, if arr[mid] is greater than the key, search in the first half otherwise search in the second half.
  • Return -1 as the default condition.

Analysis

  • Time Complexity: O(log(n))
  • Space Complexity: O(1)

Implementation

C++
int findStart(vector<int> arr, int low, int high, int key) {
   if (high >= low) {
       int mid = (high + low) / 2;
       if ((mid == 0 || key > arr[mid - 1]) && arr[mid] == key) {
           return mid;
       } else if (key > arr[mid]) {
           return findStart (arr, mid + 1, high, key);
       } else {
           return findStart (arr, low, mid - 1, key);
       }
   }
   return - 1;
}
int findEnd(vector<int> arr, int low, int high, int key, int n) {
   if (high >= low) {
       int mid = (high + low) / 2;
       if ((mid == n - 1 || key < arr[mid + 1]) && arr[mid] == key) {
           return mid;
       } else if (key < arr[mid]) {
           return findEnd (arr, low, mid - 1, key, n);
       } else {
           return findEnd (arr, mid + 1, high, key, n);
       }
   }
   return - 1;
}
vector<int> searchRange(vector<int> &arr, int key) {
   int n = arr.size();
   vector<int> indexes = {findStart (arr, 0, n - 1, key), findEnd (arr, 0, n - 1, key, n)};
   return indexes;
}
Java
class Solution {
   int findStart (int[] arr, int low, int high, int key) {
       if (high >= low) {
           int mid = (high + low) / 2;
           if ((mid == 0 || key > arr[mid - 1]) && arr[mid] == key) {
               return mid;
           } else if (key > arr[mid]) {
               return findStart(arr, mid + 1, high, key);
           } else {
               return findStart(arr, low, mid - 1, key);
           }
       }
       return -1;
   }
   int findEnd (int[] arr, int low, int high, int key, int n) {
       if (high >= low) {
           int mid = (high + low) / 2;
           if ((mid == n - 1 || key < arr[mid + 1]) && arr[mid] == key) {
               return mid;
           } else if (key < arr[mid]) {
               return findEnd(arr, low, mid - 1, key, n);
           } else {
               return findEnd(arr, mid + 1, high, key, n);
           }
       }
       return -1;
   }
   int[] searchRange (int[] arr, int key) {
       int n = arr.length;
       int[] indexes = {findStart (arr, 0, n-1, key), findEnd(arr, 0, n-1, key, n)};
       return indexes;
   }
}
Related Content
Contains Element?
Insert Position in Sorted Array
Is Perfect Square
Matrix Search
Median of Row-wise Sorted Matrix
Negative numbers in sorted array
Next Greater Element In Sorted Array
Non-Repeating Element
Search Rotated Sorted Array
Square Root
SDE Bootcamp - Become a software engineer at a product-based company
Practice Data Structures & Algorithms
Learning Resources
Interview Prep Resources
Community
Join our community
Blog
  • Career Advice and Roadmaps
  • Data Structures & Algorithms
  • Machine Coding Round (LLD)
  • System Design & Architecture
  • Backend Development
  • Frontend Development
  • Awesome Project Ideas
  • Core Computer Science
Practice Questions
  • Machine Coding (LLD) Questions
  • System Design (HLD) Questions
  • Topic-wise DSA Questions
  • Company-wise DSA Questions
  • DSA Sheets (Curated Lists)
  • JavaScript Interview Questions
  • Frontend UI Machine Coding Questions
Online Compilers (IDE)
  • Online Java Compiler
  • Online C++ Compiler
  • Online C Compiler
  • Online Python Compiler
  • Online JavaScript Compiler
Topic-wise Problems
  • Dynamic Programming Interview Questions
  • Linked List Interview Questions
  • Graph Interview Questions
  • Backtracking Interview Questions
  • Arrays Interview Questions
  • Trees Interview Questions
Company-wise Problems
  • Amazon Interview Questions
  • Microsoft Interview Questions
  • Google Interview Questions
  • Flipkart Interview Questions
  • Adobe Interview Questions
  • Facebook Interview Questions
DSA Sheets (Curated Lists)
  • Top Interview Questions
  • FAANG Interview Questions
  • Most Asked Interview Questions
  • 6 month DSA Practice Sheet
  • 3 month DSA Practice Sheet
  • Last minute DSA Practice Sheet