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
4d
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 PayPal SDE-2 interview questions & answers
Popular interview questions of SDE-2
Stay ahead in your career. Get AmbitionBox app
Helping over 1 Crore job seekers every month in choosing their right fit company
65 L+
Reviews
4 L+
Interviews
4 Cr+
Salaries
1 Cr+
Users/Month
Contribute to help millions
Get AmbitionBox app