Skip to main content
Back to problems
Leetcode
Medium
Arrays
Binary Search
Ordered Structures
Find Building Where Alice And Bob Can Meet

Answer queries about the leftmost building where two people can meet given building heights and movement rules.

Acceptance 0%
Problem Statement

You are given an array of building heights. For each query involving Alice and Bob, determine the leftmost building index they can both reach under the problem's movement rules, or report that no meeting building exists.

A meeting building must be reachable by both people according to the rules implied by the query. When multiple buildings satisfy the condition, return the one with the smallest index.

This is an array-query problem that typically requires preprocessing the heights and answering each query efficiently.

Input Format

  • An integer array heights representing the height of each building.
  • A list of queries, where each query contains the starting building positions for Alice and Bob.

The exact input encoding may vary by platform; the task is to process every query independently using the given building heights.

Output Format

  • For each query, return the index of the leftmost building where Alice and Bob can meet.
  • If no such building exists, return -1.

Constraints

  • 1 <= heights.length.
  • Building indices are valid for the given array.
  • Solve efficiently for multiple queries.

Exact official limits are not provided in the source metadata.

Examples
Sample cases returned by the problem API.

Example 1

Input

heights = [6,4,8,5,2,7], queries = [[0,1],[0,3],[2,4]]

Output

[2,5,5]

Explanation

For the first query, building 2 is the leftmost building both can reach. For the second and third queries, building 5 is the first valid meeting point.

Example 2

Input

heights = [5,3,8,2,6,1,4,6], queries = [[0,7],[3,5],[1,2]]

Output

[7,6,2]

Explanation

Each query asks for the leftmost index that satisfies the reachability condition for both people. The answer is the smallest valid index for each pair.

Premium problem context

Unlock deeper context for this problem

Premium adds guided hints, editorial links, similar variants, discussion resources, and concept maps so you can understand why a problem matters, not just solve it once.

Guided hints
Editorial and discussion links
Concept map and variants
Sign in to unlock
Track your progress
Sign in to bookmark this problem, save notes, and manage its revision plan.