755. Reach a Number
MediumSolutions in Python, Java, C++, TypeScript
Read the full problem statement on LeetCode and try it first. Below is how to approach it in an interview, followed by a complete answer in each language.
Approach
The problem asks for the minimum number of moves (numMoves) to reach a target position on an infinite number line, starting from 0. On the i-th move, we can step i units to the left or right.
Solution
Reach a Number in Python
class Solution:
def reachNumber(self, target: int) -> int:
# Symmetrically, reaching target is the same as reaching -target.
# So we can work with the absolute value.
target = abs(target)
# k is the number of moves. Python integers handle arbitrary size.
k = 0
current_sum = 0
# Step 1: Find the smallest k such that current_sum (1+2+...+k) >= target
while current_sum < target:
k += 1
current_sum += k
# Step 2: Ensure the parity matches.
# The difference between current_sum and target must be even.
# If we flip a move 'i', the sum changes by 2*i.
# This means we can only change the sum by an even number.
# So, current_sum and target must have the same parity.
# (current_sum - target) % 2 == 0 is equivalent to current_sum % 2 == target % 2.
while (current_sum - target) % 2 != 0:
k += 1
current_sum += k
return k
Reach a Number in Java
class Solution {
public int reachNumber(int target) {
// Symmetrically, reaching target is the same as reaching -target.
// So we can work with the absolute value.
target = Math.abs(target);
// k is the number of moves. We use long for sum to avoid overflow.
int k = 0;
long sum = 0;
// Step 1: Find the smallest k such that sum (1+2+...+k) >= target
while (sum < target) {
k++;
sum += k;
}
// Step 2: Ensure the parity matches.
// The difference between sum and target must be even.
// If we flip a move 'i', the sum changes by 2*i.
// This means we can only change the sum by an even number.
// So, sum and target must have the same parity.
// (sum - target) % 2 == 0 is equivalent to sum % 2 == target % 2.
while ((sum - target) % 2 != 0) {
k++;
sum += k;
}
return k;
}
}
Reach a Number in C++
class Solution {
public:
int reachNumber(int target) {
// Take the absolute value of target to simplify the problem to one direction.
// abs(INT_MIN) can cause overflow, so it's safer to use a larger type.
long t_long = abs((long)target);
long k = 0;
long sum = 0;
// Step 1: Find the first k such that 1+2+...+k >= t_long
while (sum < t_long) {
k++;
sum += k;
}
// Step 2: Ensure the parity matches.
// The difference between our sum and the target must be an even number.
// This is because flipping any move 'i' to '-i' changes the sum by -2*i, which is even.
// So, the parity of the sum (1+...+k) must match the parity of the target.
// If `sum - t_long` is odd, we need to take more steps until the difference becomes even.
while ((sum - t_long) % 2 != 0) {
k++;
sum += k;
}
return (int)k;
}
};
Reach a Number in TypeScript
function reachNumber(target: number): number {
// Symmetrically, reaching target is the same as reaching -target.
// So we can work with the absolute value.
target = Math.abs(target);
// k is the number of moves. JS numbers are 64-bit floats, safe for these values.
let k = 0;
let sum = 0;
// Step 1: Find the smallest k such that sum (1+2+...+k) >= target
while (sum < target) {
k++;
sum += k;
}
// Step 2: Ensure the parity matches.
// The difference between sum and target must be even.
// If we flip a move 'i', the sum changes by 2*i.
// This means we can only change the sum by an even number.
// So, sum and target must have the same parity.
// (sum - target) % 2 == 0 is equivalent to sum % 2 == target % 2.
while ((sum - target) % 2 !== 0) {
k++;
sum += k;
}
return k;
};
More Medium LeetCode answers
Preparing for interviews beyond LeetCode? Practise system design, SQL and behavioral questions with an in-browser runner on KatInterview.
LeetCode is a trademark of LeetCode LLC. KatInterview is not affiliated with LeetCode.