Moving Stones Until Consecutive solution and explanation
9 more videos on this problem
Leetcode 1033- Math | Moving Stones | Brain Teaser and Math CalculationsNideesh Terapalli · 8:15 · 1K views
Vol85 - Leetcode - Problem 1033 - Moving stones until consecutive - Golang - Phỏng vấn - Thuật toánYuh lập trình viên · 22:49 · 842 views
Leetcode solving: 1033. Moving Stones Until ConsecutiveMath Geeks · 6:01 · 498 views
Moving Stones Until Consecutive | Leetcode 1033 | Approach + Code with explanation.Pirai's Academy · 8:04 · 333 views
leetCode 1033- Moving Stones Until Consecutive | JSer - algorithm & JavaScriptJSer · 18:37 · 327 views
猩猩的乐园 Leetcode 1033 Moving Stones Until Consecutive猩猩的乐园 · 5:09 · 138 views
No Sound Leetcode 1033: Moving Stones Until ConsecutiveExtremeProgrammer · 1:11 · 33 views
Moving Stones Until ConsecutiveOwen Wu · 6:35 · 10 views
LeetCode 1033: Moving Stones Until ConsecutiveAlitaCode · 26:45 · 8 views
CodeTestcaseTestcaseTest Result1033. Moving Stones Until ConsecutiveOm · 17:52 · 3 views
The problem
There are three stones in different positions on the X-axis. You are given three integers a, b, and c, the positions of the stones.
In one move, you pick up a stone at an endpoint (i.e., either the lowest or highest position stone), and move it to an unoccupied position between those endpoints. Formally, let's say the stones are currently at positions x, y, and z with x < y < z. You pick up the stone at either position x or position z, and move that stone to an integer position k, with x < k < z and k != y.
The game ends when you cannot make any more moves (i.e., the stones are in three consecutive positions).
Return an integer array answer of length 2 where:
answer[0]is the minimum number of moves you can play, andanswer[1]is the maximum number of moves you can play.
Example 1:
Input: a = 1, b = 2, c = 5 Output: [1,2] Explanation: Move the stone from 5 to 3, or move the stone from 5 to 4 to 3.
Example 2:
Input: a = 4, b = 3, c = 2 Output: [0,0] Explanation: We cannot make any moves.
Example 3:
Input: a = 3, b = 5, c = 1 Output: [1,2] Explanation: Move the stone from 1 to 4; or move the stone from 1 to 2 to 4.
Constraints:
1 <= a, b, c <= 100a,b, andchave different values.
Approach overview
Problem Overview: You are given three integers representing positions of stones on a number line. In one move, you can pick up an endpoint stone and place it anywhere between the other two stones (but not at their positions). The goal is to make the stones occupy three consecutive positions. Return the minimum and maximum number of moves required.
Approach 1: Brute Reasoning with Gap Analysis (O(1) time, O(1) space)
With only three stones, the state space is tiny. Start by sorting the positions so you always work with a ≤ b ≤ c. The gaps b - a and c - b determine everything. If both gaps are already 1, the stones are consecutive and no moves are required. If either gap equals 2, you can slide the outer stone into the single missing slot in one move. Otherwise, you need two moves because each endpoint must move once to close the larger gaps.
The maximum moves come from moving endpoint stones one step inward at a time. After sorting, the total empty spaces between the outer stones is (c - a - 1). Since two stones already occupy positions inside that range, the number of remaining empty slots that must be filled through moves is (c - a - 2). Each move reduces this gap by exactly one, giving the maximum move count.
Approach 2: Sorting and Gap Calculation (Optimal) (O(1) time, O(1) space)
The clean solution sorts the three values and directly computes answers using simple arithmetic. After sorting, compute gap1 = b - a and gap2 = c - b. The minimum moves follow three cases: if gap1 == 1 and gap2 == 1, return 0; if either gap is ≤ 2, return 1; otherwise return 2. The maximum moves always equal (c - a - 2), which counts how many empty positions remain between the outer stones.
This works because every valid move must involve an endpoint stone, and the internal stone never moves. The structure of the problem collapses into simple arithmetic on the sorted coordinates rather than simulation. The logic falls under math reasoning and classic brainteaser pattern recognition rather than heavy data structures.
Recommended for interviews: Interviewers expect the sorting and gap calculation approach. It shows you recognize the invariant created by the three sorted positions and convert the game into a constant‑time math problem. Walking through the brute reasoning first demonstrates understanding of the move rules, while the final O(1) formula shows strong problem‑solving intuition.
Approach 1: Sorting and Gap Calculation
In this approach, we first sort the stone positions to determine the endpoints and calculate the gaps between consecutive stones. The minimum number of moves is determined by the distance between consecutive stones. If they're consecutive, no move is needed. For maximum moves, focus on bridging the largest gap first.
def minMaxMoves(a, b, c):
x, y, z = sorted([a, b, c])
min_moves = 0 if x + 1 == y and y + 1 == z else 1 if z - x <= 2 else 2
max_moves = (z - x) - 2
return [min_moves, max_moves]
result = minMaxMoves(1, 2, 5)
print(result)import java.util.Arrays;
public class StoneMoves {
public static int[] minMaxMoves(int a, int b, int c) {
int[] positions = {a, b, c};
Arrays.sort(positions);
int x = positions[0], y = positions[1], z = positions[2];
int minMoves = (y - x == 1 && z - y == 1) ? 0 : ((z - x <= 2) ? 1 : 2);
int maxMoves = (z - x) - 2;
return new int[]{minMoves, maxMoves};
}
public static void main(String[] args) {
int[] result = minMaxMoves(1, 2, 5);
System.out.println("[" + result[0] + "," + result[1] + "]");
}
}#include <iostream>
#include <algorithm>
using namespace std;
pair<int, int> minMaxMoves(int a, int b, int c) {
int positions[] = {a, b, c};
sort(positions, positions + 3);
int x = positions[0], y = positions[1], z = positions[2];
int d1 = y - x - 1, d2 = z - y - 1;
int minMoves = (d1 == 0 || d2 == 0) ? 0 : (d1 <= 1 || d2 <= 1) ? 1 : 2;
int maxMoves = d1 + d2;
return make_pair(minMoves, maxMoves);
}
int main() {
int a = 1, b = 2, c = 5;
auto result = minMaxMoves(a, b, c);
cout << "[" << result.first << "," << result.second << "]\n";
return 0;
}function minMaxMoves(a, b, c) {
const pos = [a, b, c].sort((a, b) => a - b);
const [x, y, z] = pos;
const minMoves = (y - x === 1 && z - y === 1) ? 0 : ((z - x <= 2) ? 1 : 2);
const maxMoves = (z - x) - 2;
return [minMoves, maxMoves];
}
console.log(minMaxMoves(1, 2, 5));#include <stdio.h>
void minMaxMoves(int a, int b, int c, int result[]) {
int x = a, y = b, z = c;
if (x > y) { int temp = x; x = y; y = temp; }
if (y > z) { int temp = y; y = z; z = temp; }
if (x > y) { int temp = x; x = y; y = temp; }
int d1 = y - x - 1, d2 = z - y - 1;
result[0] = (d1 == 0 || d2 == 0) ? 0 : (d1 <= 1 || d2 <= 1) ? 1 : 2;
result[1] = d1 + d2;
}
int main() {
int a = 1, b = 2, c = 5;
int result[2];
minMaxMoves(a, b, c, result);
printf("[%d,%d]\n", result[0], result[1]);
return 0;
}using System;
public class StoneMoves {
public static int[] MinMaxMoves(int a, int b, int c) {
int[] positions = {a, b, c};
Array.Sort(positions);
int x = positions[0], y = positions[1], z = positions[2];
int minMoves = (y - x == 1 && z - y == 1) ? 0 : (z - x <= 2) ? 1 : 2;
int maxMoves = (z - x) - 2;
return new int[] {minMoves, maxMoves};
}
public static void Main() {
int[] result = MinMaxMoves(1, 2, 5);
Console.WriteLine("[{0},{1}]", result[0], result[1]);
}
}In this Python approach, the list of positions is sorted and the gaps are used to quickly determine the edge cases for 0 moves and calculate the potential maximum by spanning the given range.
Time Complexity: O(1)
Space Complexity: O(1)
Approach 2: Default Approach
function numMovesStones(a: number, b: number, c: number): number[] {
const x = Math.min(a, Math.min(b, c));
const z = Math.max(a, Math.max(b, c));
const y = a + b + c - x - z;
let mi = 0,
mx = 0;
if (z - x > 2) {
mi = y - x < 3 || z - y < 3 ? 1 : 2;
mx = z - x - 2;
}
return [mi, mx];
}func numMovesStones(a int, b int, c int) []int {
x := min(a, min(b, c))
z := max(a, max(b, c))
y := a + b + c - x - z
mi, mx := 0, 0
if z-x > 2 {
mi = 2
if y-x < 3 || z-y < 3 {
mi = 1
}
mx = z - x - 2
}
return []int{mi, mx}
}Complexity comparison
| Approach | Complexity |
|---|---|
| Sorting and Gap Calculation | Time Complexity: O(1) |
| Default Approach | – |
Complexity analysis
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Brute Reasoning with Gap Analysis | O(1) | O(1) | Useful when first reasoning about how endpoint moves affect gaps between stones |
| Sorting and Gap Calculation (Optimal) | O(1) | O(1) | Best approach for interviews and production; compute min and max moves directly from sorted positions |
Frequently asked questions
Is Moving Stones Until Consecutive easy or hard?
Moving Stones Until Consecutive is generally considered a medium difficulty problem. The implementation is simple once the gap insight is discovered, but recognizing the special cases for the minimum moves makes it a small brainteaser.
Moving Stones Until Consecutive Python/Java solution
The Python or Java implementation sorts the three inputs and computes two values: the minimum moves using gap conditions and the maximum moves using (c − a − 2). Since the logic is constant time, the code is short and relies mainly on sorting and simple conditional checks.
How to solve Moving Stones Until Consecutive in O(1)?
Sort the three stone positions first. If both gaps (b − a and c − b) equal 1, the stones are already consecutive so the minimum moves is 0. If either gap is ≤ 2, the minimum is 1; otherwise it is 2. The maximum moves always equal (c − a − 2), which counts the empty spaces between the outer stones.
What is the best approach for Moving Stones Until Consecutive?
The optimal approach sorts the three stone positions and analyzes the gaps between them. After sorting a ≤ b ≤ c, compute gap1 = b − a and gap2 = c − b. The minimum moves depend on special cases where a gap equals 1 or 2, while the maximum moves equal (c − a − 2). This approach runs in O(1) time and O(1) space.
Is Moving Stones Until Consecutive asked at Google/Amazon/Meta?
Problems like Moving Stones Until Consecutive appear in interview prep sets for companies that test mathematical reasoning and edge‑case handling. Variants of small simulation or math brainteasers are commonly reported in interviews at companies like Amazon, Google, and Meta.
What data structure is used in Moving Stones Until Consecutive?
No advanced data structure is required. The solution only sorts three integers and performs arithmetic on the gaps between them, making it primarily a math and brainteaser problem.
What is the time complexity of Moving Stones Until Consecutive?
The optimal solution runs in O(1) time because it only sorts three numbers and performs constant arithmetic checks. Space complexity is also O(1) since no additional data structures are required.
