Given an array (1-based index) with the following elements:Array=[2,12,4,1,15,21,11,7,1,45,18,22,25]You have to perform two types of operations:Query in the form of Q(L, R): You will be given two integers L and R (1 <= L <= R <= 13). You have to find the summation of values within the index L to R inclusive using Segment Tree.For example Q(2,4) = 12+4+1 = 17Now answer the following questions: i) Tabulate the sparse table ii) Perform Query Q(3,10) iii) Perform Query Q(2,12)
Question
Given an array (1-based index) with the following elements:Array=[2,12,4,1,15,21,11,7,1,45,18,22,25]You have to perform two types of operations:Query in the form of Q(L, R): You will be given two integers L and R (1 <= L <= R <= 13). You have to find the summation of values within the index L to R inclusive using Segment Tree.For example Q(2,4) = 12+4+1 = 17Now answer the following questions: i) Tabulate the sparse table ii) Perform Query Q(3,10) iii) Perform Query Q(2,12)
Solution
i) To tabulate the sparse table, we first need to build a segment tree. A segment tree for the given array would look like this:
Level 0: [147]
Level 1: [34, 113]
Level 2: [18, 16, 57, 56]
Level 3: [14, 4, 16, 0, 29, 28, 56, 0]
Level 4: [2, 12, 4, 0, 0, 16, 0, 0, 15, 14, 28, 0, 0, 56, 0, 0]
ii) To perform the query Q(3,10), we sum the values from index 3 to 10 in the array. Using the segment tree, we can find this sum more efficiently. The sum of the values from index 3 to 10 is 4 + 1 + 15 + 21 + 11 + 7 + 1 = 60.
iii) To perform the query Q(2,12), we sum the values from index 2 to 12 in the array. Using the segment tree, we can find this sum more efficiently. The sum of the values from index 2 to 12 is 12 + 4 + 1 + 15 + 21 + 11 + 7 + 1 + 45 + 18 + 22 = 156.
Similar Questions
Given an integer array nums, handle multiple queries of the following type: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]). Example 1:Input["NumArray", "sumRange", "sumRange", "sumRange"][[[-2, 0, 3, -5, 2, -1]], [0, 2], [2, 5], [0, 5]]Output[null, 1, -1, -3]ExplanationNumArray numArray = new NumArray([-2, 0, 3, -5, 2, -1]);numArray.sumRange(0, 2); // return (-2) + 0 + 3 = 1numArray.sumRange(2, 5); // return 3 + (-5) + 2 + (-1) = -1numArray.sumRange(0, 5); // return (-2) + 0 + 3 + (-5) + 2 + (-1) = -3 Constraints:1 <= nums.length <= 104-105 <= nums[i] <= 1050 <= left <= right < nums.lengthAt most 104 calls will be made to sumRange.
prefixsumsWe have an array A of N integers. We also have Q queries, with each query consisting of two numbers, l and r.Your solution should output the sum of numbers from A[l] to A[r] (1-indexed).Constraints:1 ≤ N, Q, A[i] ≤ 1e6 for 1 ≤ i ≤ NInput:First line: N and Q, the size of the array A and the number of queries respectively.Subsequent line, N integers, the array A.Subsequent Q lines, 2 integers each, l and r.Output:For each testcase, output the answer.Sample Input:13 1111 3 14 12 14 1 8 9 1 1 5 14 41 116 81 24 116 137 101 910 136 133 73 10Sample Output:7918145143197324434960
You are given an integer array nums sorted in non-decreasing order.Build and return an integer array result with the same length as nums such that result[i] is equal to the summation of absolute differences between nums[i] and all the other elements in the array.In other words, result[i] is equal to sum(|nums[i]-nums[j]|) where 0 <= j < nums.length and j != i (0-indexed). Example 1:Input: nums = [2,3,5]Output: [4,3,5]Explanation: Assuming the arrays are 0-indexed, thenresult[0] = |2-2| + |2-3| + |2-5| = 0 + 1 + 3 = 4,result[1] = |3-2| + |3-3| + |3-5| = 1 + 0 + 2 = 3,result[2] = |5-2| + |5-3| + |5-5| = 3 + 2 + 0 = 5.Example 2:Input: nums = [1,4,6,8,10]Output: [24,15,13,15,21]
You are given a 0-indexed integer array nums having length n, an integer indexDifference, and an integer valueDifference.Your task is to find two indices i and j, both in the range [0, n - 1], that satisfy the following conditions:abs(i - j) >= indexDifference, andabs(nums[i] - nums[j]) >= valueDifferenceReturn an integer array answer, where answer = [i, j] if there are two such indices, and answer = [-1, -1] otherwise. If there are multiple choices for the two indices, return any of them.Note: i and j may be equal. Example 1:Input: nums = [5,1,4,1], indexDifference = 2, valueDifference = 4Output: [0,3]Explanation: In this example, i = 0 and j = 3 can be selected.abs(0 - 3) >= 2 and abs(nums[0] - nums[3]) >= 4.Hence, a valid answer is [0,3].[3,0] is also a valid answer.Example 2:Input: nums = [2,1], indexDifference = 0, valueDifference = 0Output: [0,0]Explanation: In this example, i = 0 and j = 0 can be selected.abs(0 - 0) >= 0 and abs(nums[0] - nums[0]) >= 0.Hence, a valid answer is [0,0].Other valid answers are [0,1], [1,0], and [1,1].Example 3:Input: nums = [1,2,3], indexDifference = 2, valueDifference = 4Output: [-1,-1]Explanation: In this example, it can be shown that it is impossible to find two indices that satisfy both conditions.Hence, [-1,-1] is returned. Constraints:1 <= n == nums.length <= 1050 <= nums[i] <= 1090 <= indexDifference <= 1050 <= valueDifference <= 109
Java program to sum values of an array.Write a Java program to sum values of an array. Constraints:N/AExample:Input:1, 2, 3, 4, 5, 6, 7, 8, 9, 10Output:55Explanation:-Public Test Cases:# INPUT EXPECTED OUTPUT1 1, 2, 3, 4, 5, 6, 7, 8, 9, 1055
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.