Sum Queries in a Sorted Array
Given two arrays arr
and queries
, your task is to determine the sum of all elements in arr
that are less than or equal to each element in queries
. The array arr
is provided in sorted ascending order.
Input:
The first line indicates an integer ‘T’, the number of test cases.
For each test case, the first line contains two integers, ‘n’ (size of ‘arr’) and ‘q’ (size of ‘queries’).
The second line specifies ‘n’ integers, the elements of array ‘arr’.
The third line specifies ‘q’ integers, the elements of array ‘queries’.
Output:
For each test case, output a list of integers where each integer is the sum of array elements <= each query. Each test case’s output should be on a separate line.
Example:
Input:
T = 1 arr = [1, 2, 3, 3, 4, 5, 6, 7, 9, 10] queries = [3, 5]
Output:
[9, 18]
Constraints:
1 <= T <= 100
1 <= N <= 1000
1 <= Q <= 1000
-10^9 <= arr[i] <= 10^9
-10^9 <= query[i] <= 10^9
Note:
The function implementation should directly return the results for each test case without printing, as the printing is managed externally.

AnswerBot
4mo
Find sum of elements in a sorted array less than or equal to each element in queries.
Iterate through queries and for each query, find the sum of elements in arr less than or equal to the query element...read more
Help your peers!
Add answer anonymously...
Top SDE-2 Interview Questions Asked at PayPal
Q. Given a sorted array of integers, find the leftmost and rightmost index of a tar...read more
Q. Minimum Character Deletion Problem Statement You have been provided a string STR...read more
Q. Palindrome Checker Problem Statement Given an alphabetical string S, determine w...read more
Interview Questions Asked to SDE-2 at Other Companies
Top Skill-Based Questions for PayPal SDE-2
Algorithms Interview Questions and Answers
250 Questions
Data Structures Interview Questions and Answers
250 Questions
System Design Interview Questions and Answers
250 Questions
Web Development Interview Questions and Answers
250 Questions
Operating Systems Interview Questions and Answers
250 Questions
Java Interview Questions and Answers
250 Questions
Stay ahead in your career. Get AmbitionBox app


Trusted by over 1.5 Crore job seekers to find their right fit company
80 L+
Reviews
10L+
Interviews
4 Cr+
Salaries
1.5 Cr+
Users
Contribute to help millions
AmbitionBox Awards
Get AmbitionBox app

