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.

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!
Select
Add answer anonymously...

PayPal SDE-2 interview questions & answers

A SDE-2 was asked 10mo agoQ. Given a sorted array of integers, find the leftmost and rightmost index of a tar...read more
A SDE-2 was asked Q. Minimum Character Deletion Problem Statement You have been provided a string STR...read more
A SDE-2 was asked Q. Palindrome Checker Problem Statement Given an alphabetical string S, determine w...read more

Popular interview questions of SDE-2

A SDE-2 was asked 10mo agoQ1. Given a sorted array of integers, find the leftmost and rightmost index of a tar...read more
A SDE-2 was asked Q2. Minimum Character Deletion Problem Statement You have been provided a string STR...read more
A SDE-2 was asked Q3. Palindrome Checker Problem Statement Given an alphabetical string S, determine w...read more
PayPal SDE-2 Interview Questions
Stay ahead in your career. Get AmbitionBox app
play-icon
play-icon
qr-code
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

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

Follow Us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter
Profile Image
Hello, Guest
AmbitionBox Employee Choice Awards 2025
Winners announced!
awards-icon
Contribute to help millions!
Write a review
Write a review
Share interview
Share interview
Contribute salary
Contribute salary
Add office photos
Add office photos
Add office benefits
Add office benefits