Moving Stones Until Consecutive - Problem
Moving Stones Until Consecutive

Imagine you have three stones placed at different positions on a number line. Your goal is to move them until they occupy three consecutive positions (like positions 5, 6, 7).

๐ŸŽฏ The Rules:
โ€ข You can only pick up stones from the endpoints (leftmost or rightmost stone)
โ€ข You can only place a stone in an unoccupied position between the current leftmost and rightmost stones
โ€ข The game ends when all three stones are in consecutive positions

๐Ÿ“Š Your Task:
Given three integers a, b, and c representing the initial positions of the stones, return an array [min_moves, max_moves] where:
โ€ข min_moves is the minimum number of moves needed
โ€ข max_moves is the maximum number of moves possible

Example: If stones are at positions [1, 2, 5], you could move the stone at 5 to position 3 in just 1 move to get [1, 2, 3]. But you could also make more moves by moving strategically to maximize the total number of moves.

Input & Output

example_1.py โ€” Basic Case
$ Input: 1 2 5
โ€บ Output: [1, 2]
๐Ÿ’ก Note: Stones at [1,2,5]. Min: Move stone 5 to position 3 in 1 move. Max: Use both empty spaces (3,4) in 2 moves total.
example_2.py โ€” Already Consecutive
$ Input: 4 3 2
โ€บ Output: [0, 0]
๐Ÿ’ก Note: Stones are already at consecutive positions [2,3,4] when sorted. No moves needed.
example_3.py โ€” Large Gaps
$ Input: 1 8 10
โ€บ Output: [2, 7]
๐Ÿ’ก Note: Min: Need 2 moves to make consecutive. Max: Can use all 7 empty spaces (positions 2,3,4,5,6,7,9) strategically.

Visualization

Tap to expand
Stone Movement Strategy VisualizationExample: Stones at positions [1, 2, 5]0123456Stone AStone BStone CMinimum Moves = 1โœ“ Stones A and B are already consecutive (positions 1,2)โ†’ Just move Stone C from position 5 to position 3Maximum Moves = 2โ€ข Empty spaces between stones: positions 3 and 4 (total: 2 spaces)โ€ข Strategy: Use both spaces by moving stones strategicallyโ€ข Example: [1,2,5] โ†’ [1,2,4] โ†’ [2,3,4] (2 moves total)๐Ÿ’ก Key Insight:This is a pure math problem! No need to simulate moves.Just analyze the gaps between sorted positions to get both answers instantly.
Understanding the Visualization
1
Identify the Situation
Sort the positions to see left, middle, right arrangement and measure gaps
2
Quick Win Check
If any two people are already next to each other, just 1 move needed
3
Calculate Minimum
Special cases for close positions, otherwise 2 moves in general
4
Calculate Maximum
Count all empty spaces that could be used strategically
Key Takeaway
๐ŸŽฏ Key Insight: This problem is solved through mathematical gap analysis rather than move simulation. The minimum depends on existing consecutive stones, while the maximum equals total empty spaces between the outermost stones.

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(1)

Just a few mathematical calculations regardless of stone positions

n
2n
โœ“ Linear Growth
Space Complexity
O(1)

Only using a constant amount of extra space

n
2n
โœ“ Linear Space

Constraints

  • 1 โ‰ค a, b, c โ‰ค 100
  • a, b, and c have different values
  • The stones are on a number line with integer positions
Asked in
Google 25 Facebook 18 Amazon 15 Microsoft 12
27.8K Views
Medium Frequency
~15 min Avg. Time
892 Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen