Find an Index by Halving a Sorted Range

Quick Overview

Find an Index by Halving a Sorted Range. Search the strictly increasing array nums for target. Return its zero-based index if present, otherwise -1.

Find an Index by Halving a Sorted Range

Company: Google

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: easy

Interview Round: Other

### Problem Search the strictly increasing array nums for target. Return its zero-based index if present, otherwise -1. The search must take O(log n) time. ```cpp class Solution { public: int search(vector<int>& nums, int target); }; ``` ### Examples **Example 1:** ```text nums = [-4,1,6,10], target = 6 Output: 2 Explanation: nums[2] equals the target. ``` **Example 2:** ```text nums = [-4,1,6,10], target = 5 Output: -1 Explanation: No array position holds 5. ``` ### Constraints - There are 1 through 10000 entries, in strictly increasing order. - Both entries and target are strictly between -10000 and 10000. ### Contract considerations Preserve the required logarithmic time and return an index. ```hint Read the boundary condition What half can be eliminated by a single midpoint comparison? ``` ### Boundary reminders Preserve the required logarithmic time and return an index.

Overview: Find an Index by Halving a Sorted Range. Search the strictly increasing array nums for target. Return its zero-based index if present, otherwise -1.

Google logo
Google
Source window: 0–3 months · as of Oct 21, 2026
easySoftware EngineerOtherCoding & Algorithms
0
0

Problem

Search the strictly increasing array nums for target. Return its zero-based index if present, otherwise -1. The search must take O(log n) time.

class Solution {
public:
    int search(vector<int>& nums, int target);
};

Examples

Example 1:

nums = [-4,1,6,10], target = 6
Output: 2
Explanation: nums[2] equals the target.

Example 2:

nums = [-4,1,6,10], target = 5
Output: -1
Explanation: No array position holds 5.

Constraints

  • There are 1 through 10000 entries, in strictly increasing order.
  • Both entries and target are strictly between -10000 and 10000.

Contract considerations

Preserve the required logarithmic time and return an index.

Boundary reminders

Preserve the required logarithmic time and return an index.