Skip to main content

Command Palette

Search for a command to run...

Find the Pivot Integer

Published
2 min read
Find the Pivot Integer
G

As a Systems Engineer at Tata Consultancy Services, I deliver exceptional software products for mobile and web platforms, using agile methodologies and robust quality maintenance. I am experienced in performance testing, automation testing, API testing, and manual testing, with various tools and technologies such as Jmeter, Azure LoadTest, Selenium, Java, OOPS, Maven, TestNG, and Postman.

I have successfully developed and executed detailed test plans, test cases, and scripts for Android and web applications, ensuring high-quality standards and user satisfaction. I have also demonstrated my proficiency in manual REST API testing with Postman, as well as in end-to-end performance and automation testing using Jmeter and selenium with Java, TestNG and Maven. Additionally, I have utilized Azure DevOps for bug tracking and issue management.

Given a positive integer n, find the pivot integer x such that:

  • The sum of all elements between 1 and x inclusively equals the sum of all elements between x and n inclusively.

Return the pivot integer x. If no such integer exists, return -1. It is guaranteed that there will be at most one pivot index for the given input.

LeetCode Problem - 2485

class Solution {
    // Method to find the pivot integer in the range [1, n]
    public int pivotInteger(int n) {
        int sum1 = 0;
        // Calculate the sum of integers from 1 to n
        for (int i = 1; i <= n; i++){
            sum1 += i;
            // Calculate the sum of integers from i to n using xTon method
            int sum2 = xTon(i, n);
            // Check if the sums are equal, indicating the pivot integer
            if (sum1 == sum2){
                return i;
            }
        }
        // If no pivot integer is found, return -1
        return -1;
    }

    // Declare a variable to store the sum calculated in xTon method
    int temp2 = 0;

    // Method to calculate the sum of integers from b to n
    public int xTon(int b, int n){
        // Initialize the sum variable
        temp2 = 0;
        // Calculate the sum of integers from b to n
        for (int i = b; i <= n; i++){
            temp2 += i;
        }
        // Return the sum
        return temp2;
    }
}

More from this blog

S

Software and Performance Testing Insights

462 posts

Results-Driven Agile QA Specialist | Expert in Mobile & Web Testing | Proficient in Test Planning, Execution, and Root Cause Analysis.