1040. Moving Stones Until Consecutive II LeetCode Solution
In this guide, you will get 1040. Moving Stones Until Consecutive II LeetCode Solution with the best time and space complexity. The solution to Moving Stones Until Consecutive II problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.
Table of Contents
- Problem Statement
- Complexity Analysis
- Moving Stones Until Consecutive II solution in C++
- Moving Stones Until Consecutive II solution in Java
- Moving Stones Until Consecutive II solution in Python
- Additional Resources

Problem Statement of Moving Stones Until Consecutive II
There are some stones in different positions on the X-axis. You are given an integer array stones, the positions of the stones.
Call a stone an endpoint stone if it has the smallest or largest position. In one move, you pick up an endpoint stone and move it to an unoccupied position so that it is no longer an endpoint stone.
In particular, if the stones are at say, stones = [1,2,5], you cannot move the endpoint stone at position 5, since moving it to any position (such as 0, or 3) will still keep that stone as an endpoint stone.
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, and
answer[1] is the maximum number of moves you can play.
Example 1:
Input: stones = [7,4,9]
Output: [1,2]
Explanation: We can move 4 -> 8 for one move to finish the game.
Or, we can move 9 -> 5, 4 -> 6 for two moves to finish the game.
Example 2:
Input: stones = [6,5,4,3,10]
Output: [2,3]
Explanation: We can move 3 -> 8 then 10 -> 7 to finish the game.
Or, we can move 3 -> 7, 4 -> 8, 5 -> 9 to finish the game.
Notice we cannot move 10 -> 2 to finish the game, because that would be an illegal move.
Constraints:
3 <= stones.length <= 104
1 <= stones[i] <= 109
All the values of stones are unique.
Complexity Analysis
- Time Complexity: O(\texttt{sort})
- Space Complexity: O(\texttt{sort})
1040. Moving Stones Until Consecutive II LeetCode Solution in C++
class Solution {
public:
vector<int> numMovesStonesII(vector<int>& stones) {
const int n = stones.size();
int minMoves = n;
ranges::sort(stones);
for (int l = 0, r = 0; r < n; ++r) {
while (stones[r] - stones[l] + 1 > n)
++l;
int alreadyStored = r - l + 1;
if (alreadyStored == n - 1 && stones[r] - stones[l] + 1 == n - 1)
minMoves = min(minMoves, 2);
else
minMoves = min(minMoves, n - alreadyStored);
}
return {minMoves, max(stones[n - 1] - stones[1] - n + 2,
stones[n - 2] - stones[0] - n + 2)};
}
};
/* code provided by PROGIEZ */
1040. Moving Stones Until Consecutive II LeetCode Solution in Java
class Solution {
public int[] numMovesStonesII(int[] stones) {
final int n = stones.length;
int minMoves = n;
Arrays.sort(stones);
for (int l = 0, r = 0; r < n; ++r) {
while (stones[r] - stones[l] + 1 > n)
++l;
int alreadyStored = r - l + 1;
if (alreadyStored == n - 1 && stones[r] - stones[l] + 1 == n - 1)
minMoves = Math.min(minMoves, 2);
else
minMoves = Math.min(minMoves, n - alreadyStored);
}
return new int[] {
minMoves, Math.max(stones[n - 1] - stones[1] - n + 2, stones[n - 2] - stones[0] - n + 2)};
}
}
// code provided by PROGIEZ
1040. Moving Stones Until Consecutive II LeetCode Solution in Python
class Solution:
def numMovesStonesII(self, stones: list[int]) -> list[int]:
n = len(stones)
minMoves = n
stones.sort()
l = 0
for r, stone in enumerate(stones):
while stone - stones[l] + 1 > n:
l += 1
alreadyStored = r - l + 1
if alreadyStored == n - 1 and stone - stones[l] + 1 == n - 1:
minMoves = 2
else:
minMoves = min(minMoves, n - alreadyStored)
return [minMoves, max(stones[n - 1] - stones[1] - n + 2, stones[n - 2] - stones[0] - n + 2)]
# code by PROGIEZ
Additional Resources
- Explore all LeetCode problem solutions at Progiez here
- Explore all problems on LeetCode website here
Happy Coding! Keep following PROGIEZ for more updates and solutions.