Reach a Signed Destination with Increasing Step Sizes

Quick Overview

Reach a Signed Destination with Increasing Step Sizes. Begin at zero. On move i, starting with i=1, travel exactly i units either left or right.

Reach a Signed Destination with Increasing Step Sizes

Company: Google

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Other

### Problem Begin at zero. On move i, starting with i=1, travel exactly i units either left or right. Return the smallest move count that can reach the nonzero signed target. ```cpp class Solution { public: int reachNumber(int target); }; ``` ### Examples **Example 1:** ```text target = 4 Output: 3 Explanation: -1+2+3=4; two moves have maximum distance 3. ``` **Example 2:** ```text target = -6 Output: 3 Explanation: -1-2-3=-6. ``` ### Constraints - Target belongs to [-1000000000,1000000000] and is not zero. ### Contract considerations Move length depends on the move number, not on the chosen direction. ```hint Read the boundary condition How does reversing one step change the all-right total? ``` ### Boundary reminders Move length depends on the move number, not on the chosen direction.

Overview: Reach a Signed Destination with Increasing Step Sizes. Begin at zero. On move i, starting with i=1, travel exactly i units either left or right.

Google logo
Google
Source window: 0–6 months · as of Oct 21, 2026
mediumSoftware EngineerOtherCoding & Algorithms
1
0

Problem

Begin at zero. On move i, starting with i=1, travel exactly i units either left or right. Return the smallest move count that can reach the nonzero signed target.

class Solution {
public:
    int reachNumber(int target);
};

Examples

Example 1:

target = 4
Output: 3
Explanation: -1+2+3=4; two moves have maximum distance 3.

Example 2:

target = -6
Output: 3
Explanation: -1-2-3=-6.

Constraints

  • Target belongs to [-1000000000,1000000000] and is not zero.

Contract considerations

Move length depends on the move number, not on the chosen direction.

Boundary reminders

Move length depends on the move number, not on the chosen direction.