Class Solution

java.lang.Object
g2401_2500.s2485_find_the_pivot_integer.Solution

public class Solution extends Object
2485 - Find the Pivot Integer.<p>Easy</p> <p>Given a positive integer <code>n</code>, find the <strong>pivot integer</strong> <code>x</code> such that:</p> <ul> <li>The sum of all elements between <code>1</code> and <code>x</code> inclusively equals the sum of all elements between <code>x</code> and <code>n</code> inclusively.</li> </ul> <p>Return <em>the pivot integer</em> <code>x</code>. If no such integer exists, return <code>-1</code>. It is guaranteed that there will be at most one pivot index for the given input.</p> <p><strong>Example 1:</strong></p> <p><strong>Input:</strong> n = 8</p> <p><strong>Output:</strong> 6</p> <p><strong>Explanation:</strong> 6 is the pivot integer since: 1 + 2 + 3 + 4 + 5 + 6 = 6 + 7 + 8 = 21.</p> <p><strong>Example 2:</strong></p> <p><strong>Input:</strong> n = 1</p> <p><strong>Output:</strong> 1</p> <p><strong>Explanation:</strong> 1 is the pivot integer since: 1 = 1.</p> <p><strong>Example 3:</strong></p> <p><strong>Input:</strong> n = 4</p> <p><strong>Output:</strong> -1</p> <p><strong>Explanation:</strong> It can be proved that no such integer exist.</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 <= n <= 1000</code></li> </ul>
  • Constructor Details

    • Solution

      public Solution()
  • Method Details

    • pivotInteger

      public int pivotInteger(int n)