Find the Integer Shared by Two Equal Inclusive Range Sums

Quick Overview

Find an integer where the inclusive sums from 1 to the pivot and from the pivot to n are equal, or return -1.

Find the Integer Shared by Two Equal Inclusive Range Sums

Company: Google

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: easy

Interview Round: Other

### Problem Find an integer `x` in `[1, n]` such that the sum from 1 through `x` equals the sum from `x` through `n`, including `x` in both sums. Return `x`, or `-1` if none exists. There is at most one answer. ```cpp class Solution { public: int pivotInteger(int n); }; ``` ### Examples **Example 1:** ```text Input: n = 49 Output: 35 Explanation: Both sums, from 1 through 35 and from 35 through 49, equal 630. ``` **Example 2:** ```text Input: n = 3 Output: -1 Explanation: The total triangular sum 6 is not a square, so no pivot exists. ``` ### Constraints - `1 <= n <= 1000`. ### Clarifying Questions - Is `x` included on both sides? Yes. - Is this an array index? No; return an integer value in 1 through n. - Can `n = 1` have an answer? Yes; both sums then contain 1. ```hint Check the boundary Check that both inclusive sums contain the candidate pivot. ```

Overview: Find an integer where the inclusive sums from 1 to the pivot and from the pivot to n are equal, or return -1.

Google logo
Google
Source window: 0–3 months · as of Oct 21, 2026
easySoftware EngineerOtherCoding & Algorithms
0
0

Problem

Find an integer x in [1, n] such that the sum from 1 through x equals the sum from x through n, including x in both sums. Return x, or -1 if none exists. There is at most one answer.

class Solution {
public:
    int pivotInteger(int n);
};

Examples

Example 1:

Input: n = 49
Output: 35
Explanation: Both sums, from 1 through 35 and from 35 through 49, equal 630.

Example 2:

Input: n = 3
Output: -1
Explanation: The total triangular sum 6 is not a square, so no pivot exists.

Constraints

  • 1 <= n <= 1000 .

Clarifying Questions Guidance

  • Is x included on both sides? Yes.
  • Is this an array index? No; return an integer value in 1 through n.
  • Can n = 1 have an answer? Yes; both sums then contain 1.