Find the minimum and maximum moves to make stones on a line consecutive

Quick Overview

Given distinct stone positions on a number line, compute the fewest and the most moves a game can last when each move relocates the smallest or largest stone so it is no longer an endpoint. Tests sorting, counting stones in a window of positions, and careful reasoning about one tricky edge case.

Find the minimum and maximum moves to make stones on a line consecutive

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: hard

Interview Round: Other

Stones sit at distinct integer positions on a number line. You are given an integer array `stones` holding their positions, in no particular order. A stone is an endpoint stone if it has the smallest or the largest position. In one move you pick up an endpoint stone and place it on an unoccupied position where it is no longer an endpoint stone. For example, with `stones = [1, 2, 5]` you cannot move the stone at `5`: wherever it goes (such as `0` or `3`), it is still the smallest or the largest stone. The game ends when no move is possible, which happens exactly when the stones occupy consecutive positions. Return the fewest moves and the most moves a complete game can take. ### Function Signature ```python def min_max_stone_moves(stones: list[int]) -> list[int]: ``` ### Rules - `stones` is not sorted. All positions are distinct integers. - A move takes the stone with the smallest or the largest position and puts it on an empty integer position strictly between the smallest and largest positions of the other stones. - A game is played until no move is possible, that is, until all `n` stones occupy `n` consecutive positions. - Return exactly two integers, `[min_moves, max_moves]`. If the stones are already consecutive, return `[0, 0]`. ### Constraints - `3 <= len(stones) <= 10^4` - `1 <= stones[i] <= 10^9` - All values in `stones` are distinct. ### Examples ```text Input: stones = [7, 4, 9] Output: [1, 2] ``` Moving 4 to 8 ends the game in one move at `[7, 8, 9]`. The longest game has two moves: 9 to 5 (between 4 and 7), then 4 to 6, ending at `[5, 6, 7]`. ```text Input: stones = [6, 5, 4, 3, 10] Output: [2, 3] ``` Moving 3 to 8 and then 10 to 7 ends at `[4, 5, 6, 7, 8]`. One move cannot finish, because 10 would have to land at 2 or 7, and both spots leave it as an endpoint. The longest game is 3 to 7, 4 to 8, 5 to 9, ending at `[6, 7, 8, 9, 10]`. ```text Input: stones = [1, 2, 5] Output: [2, 2] ``` The stone at 5 can never move first, so the first move is 1 to 3 or 1 to 4. Either one leaves a single gap that takes one more move to close (for example 1 to 3, then 2 to 4), so every game lasts exactly two moves.

Overview: Given distinct stone positions on a number line, compute the fewest and the most moves a game can last when each move relocates the smallest or largest stone so it is no longer an endpoint. Tests sorting, counting stones in a window of positions, and careful reasoning about one tricky edge case.

Nov 24, 2026
hardSoftware EngineerOtherCoding & Algorithms
0
0

Stones sit at distinct integer positions on a number line. You are given an integer array stones holding their positions, in no particular order.

A stone is an endpoint stone if it has the smallest or the largest position. In one move you pick up an endpoint stone and place it on an unoccupied position where it is no longer an endpoint stone.

For example, with stones = [1, 2, 5] you cannot move the stone at 5: wherever it goes (such as 0 or 3), it is still the smallest or the largest stone.

The game ends when no move is possible, which happens exactly when the stones occupy consecutive positions. Return the fewest moves and the most moves a complete game can take.

Function Signature

def min_max_stone_moves(stones: list[int]) -> list[int]:

Rules

  • stones is not sorted. All positions are distinct integers.
  • A move takes the stone with the smallest or the largest position and puts it on an empty integer position strictly between the smallest and largest positions of the other stones.
  • A game is played until no move is possible, that is, until all n stones occupy n consecutive positions.
  • Return exactly two integers, [min_moves, max_moves] . If the stones are already consecutive, return [0, 0] .

Constraints

  • 3 <= len(stones) <= 10^4
  • 1 <= stones[i] <= 10^9
  • All values in stones are distinct.

Examples

Input: stones = [7, 4, 9]
Output: [1, 2]

Moving 4 to 8 ends the game in one move at [7, 8, 9]. The longest game has two moves: 9 to 5 (between 4 and 7), then 4 to 6, ending at [5, 6, 7].

Input: stones = [6, 5, 4, 3, 10]
Output: [2, 3]

Moving 3 to 8 and then 10 to 7 ends at [4, 5, 6, 7, 8]. One move cannot finish, because 10 would have to land at 2 or 7, and both spots leave it as an endpoint. The longest game is 3 to 7, 4 to 8, 5 to 9, ending at [6, 7, 8, 9, 10].

Input: stones = [1, 2, 5]
Output: [2, 2]

The stone at 5 can never move first, so the first move is 1 to 3 or 1 to 4. Either one leaves a single gap that takes one more move to close (for example 1 to 3, then 2 to 4), so every game lasts exactly two moves.