Skip to main content

Command Palette

Search for a command to run...

Range Sum Query - Immutable

Published
1 min read
Range Sum Query - Immutable
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 an integer array nums, handle multiple queries of the following type:

  1. Calculate the sum of the elements of nums between indices left and right inclusive where left <= right.

Implement the NumArray class:

  • NumArray(int[] nums) Initializes the object with the integer array nums.

  • int sumRange(int left, int right) Returns the sum of the elements of nums between indices left and right inclusive (i.e. nums[left] + nums[left + 1] + ... + nums[right]).

LeetCode Problem - 303

class NumArray {
    int[] nums;  // Array to hold the cumulative sum of elements

    public NumArray(int[] nums) {
        // Compute the cumulative sum array
        // nums[i] will store the sum of the elements from index 0 to i
        for (int i = 1; i < nums.length; i++) {
            nums[i] += nums[i - 1];
        }
        // Assign the cumulative sum array to the class member variable
        this.nums = nums;
    }

    // Method to calculate the sum of elements between indices left and right (inclusive)
    public int sumRange(int left, int right) {
        // If the left index is 0, return the cumulative sum up to the right index
        if (left == 0) return nums[right];
        // Otherwise, return the difference between the cumulative sum at right and the cumulative sum just before left
        return nums[right] - nums[left - 1];
    }
}

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.