Asked inPayPal,SDE-2
Search In The Array

You are given two arrays ‘arr’ of size ‘n’ and ‘queries’ of size ‘q’. For each element ‘q’ in the array 'queries', your task is to find the sum of all elements in the array ‘arr’ which are less than or equal to ‘q’.

For example: given array ‘arr = { 1, 2, 3, 3, 4, 5, 6, 7, 9, 10}’ and ‘ queries= { 3, 5}’
Then the sum of all elements whose value is less than or equal to
‘queries[0] = 3’ is ‘ 1+2+3+3 =9’.
‘queries[1] = 5’ is ‘1+2+3+3+4+5 =18’.

You have to return the answer of every query { 9, 18}.

Input Format :
The first line of input contains an integer ‘T’ denoting the number of test cases.

The first line of each test case contains two space-separated integers ‘n’ and ‘q’, where ‘n’ denotes the number of elements in the array ‘arr’ and ‘q’ denotes the number of elements in array ‘queries’.

The second line of each test case contains ‘n’ space-separated integers denoting the elements of the array ‘arr’.

The third line of each test case contains ‘q’ space-separated integers denoting the elements of the array ‘queries’.
Output Format :
For every test case, return the arraylist/list/vector of size ‘q’ denoting the answer to each query.

Output for each test case will be printed in a separate line.
Note :
1. You do not need to print anything; it has already been taken care of. Just implement the given function.
2. Given array ‘arr’ is sorted in ascending order.
Constraint :
1 <= T <= 100
1 <= N <= 1000
1 <= Q <= 1000
-10^9 <= arr[i] <= 10^9
-10^9 <= query[i] <= 10^9

Where ‘T’ represents the number of test cases, ‘N’ is the number of elements in array ‘arr’, ‘Q’ denotes the number of elements in array ‘queries’. ‘arr[i]’ represents the value of the number at ‘i’ position in ‘arr’. ‘query[i]’ represents the value of ‘i-th’ queries.

Time Limit : 1 sec
CodingNinjas
author
2y
Linear search

For each query, we can iterate over the array ‘arr’ and find the required sum.

The algorithm will be-

  • We will use an array/list ‘answer’ to store the answer to each query.
  • Now, for each ‘i’...read more
CodingNinjas
author
2y
Binary Search

The basic idea is to store the prefix sum for every index ‘i’. We will maintain a prefix sum array in which 'prefixSum[i]’ represents all elements from ‘0’ to ‘i-th’ index.

We have to find...read more

Help your peers!
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
Get AmbitionBox app

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