Asked inPayPal,SDE-2

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.

Be the first one to answer
Add answer anonymously...
PayPal SDE-2 Interview Questions
Stay ahead in your career. Get AmbitionBox app
qr-code
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

Made with ❤️ in India. Trademarks belong to their respective owners. All rights reserved © 2024 Info Edge (India) Ltd.

Follow us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter