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
โข
โข
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.
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 possibleExample: 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
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
โ Linear Growth
Space Complexity
O(1)
Only using a constant amount of extra space
โ 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
๐ก
Explanation
AI Ready
๐ก Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code