Skip to main content

Command Palette

Search for a command to run...

Left and Right Sum Differences

Published
1 min read
Left and Right Sum Differences
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 0-indexed integer array nums, find a 0-indexed integer array answer where:

  • answer.length == nums.length.

  • answer[i] = |leftSum[i] - rightSum[i]|.

Where:

  • leftSum[i] is the sum of elements to the left of the index i in the array nums. If there is no such element, leftSum[i] = 0.

  • rightSum[i] is the sum of elements to the right of the index i in the array nums. If there is no such element, rightSum[i] = 0.

Return the array answer.

LeetCode Problem - 2574

class Solution {
    public int[] leftRightDifference(int[] nums) {
        // Initialize an array to store the result
        int[] resultArr =  new int[nums.length];

        // Initialize arrays to store the left and right sums
        int[] rightSumArray = new int[nums.length];
        int[] leftSumArray = new int[nums.length];

        // Calculate left sums
        int temp = 0;
        for(int i=0; i<nums.length; i++){
            if(i != 0){
                temp += nums[i-1];
                leftSumArray[i] = temp;
            }
            else{
                leftSumArray[i] = temp;
            }
        }

        // Calculate right sums
        for(int i=0; i<nums.length; i++){
            temp = 0;
            for(int j=i+1; j<nums.length; j++){
                temp += nums[j];
            }
            rightSumArray[i] = temp;
        }

        // Calculate absolute differences between left and right sums and store them in resultArr
        for(int i=0; i<nums.length; i++){
            resultArr[i] = Math.abs(leftSumArray[i] - rightSumArray[i]);
        }
        return resultArr;
    }
}

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.