City of Happy People

Ninja is traveling to a Happy city where ‘N’ people reside. In the city, the happiness of each person is represented as a number ‘H’ where ‘H’ ranges from -20,000,000 to 20,000,000 (both inclusive). The happiness of each person in the city is given in an array/list ‘HAPPINESS’ where ‘HAPPINESS[i]’ represents the happiness of the i’th person.

Ninja wants to make a group of people such that the overall happiness of the group (sum of happiness value of all the people in the group) ranges between ‘A’ and ‘B’ where -500,000,000 ≤ A ≤ B ≤ 500,000,000. Ninja wants to know in how many ways he can make this group.

For example :
For the given ‘HAPPINESS[]’ = ‘[-1, 0, 3]’ and ‘A’ = 0 , ‘B’ = 2. Following are the ways to group people such that the overall happiness of the group is between ‘A’ and ‘B’:

[-1, 0, 3], the sum of all the happiness values of this group is 2.

[-1, 3], the sum of all the happiness values of this group is 2.

[0], the sum of all the happiness values of this group is 0.

[ ], the sum of all the happiness values of this empty group is 0.

So the number of ways is 4.

As Ninja is busy with some other task so he asks you for help. Can you help Ninja to find out the number of ways in which he can pick groups so that the overall happiness of the group must be in the range between ‘A’ and ‘B’?

Input Format :
The first line of input contains an integer ‘T’ which denotes the number of test cases or queries to be run. Then the test cases follow.

The first line of each test case contains three single space-separated integers ‘N’, ’A’, and ‘B’ representing the number of elements in the array/list ‘HAPPINESS’ and the range in which happiness values of groups must reside respectively.

The next line of each test case contains ‘N’ single space-separated integers denoting the  ‘HAPPINESS’ values.
Output Format :
For each test case, print the number of ways to pick groups so that the overall happiness of the group lies between ‘A’ and ‘B’ inclusive.

Print the output of each test case in a separate line.
Note:
You do not need to print anything; it has already been taken care of. Just implement the given function.
Constraints :
1 <= ‘T’ <= 100
1 <= ‘N’ <= 34
-20,000,000 ≤ ‘HAPPINESS[i]’ ≤ 20,000,000
-500,000,000 ≤ ‘A’ ≤ ‘B’ ≤ 500,000,000

Time Limit: 1 second
AnswerBot
1y

The problem is to find the number of ways to form a group of people such that the overall happiness of the group falls within a given range.

  • Iterate through all possible subsets of the given array/list...read more

CodingNinjas
author
2y
Brute Force

We know there are a total 2 ^ ‘N’ subsets possible for an array/list of ‘N’. elements. The idea behind this approach is to generate all possible subsets and then find the sum of all the hap...read more

CodingNinjas
author
2y
Binary Search & Bit masking

We can optimize the previous approach. In the brute force approach, there will be 2^34 (17179869184) subsets in the worst case. We can reduce this problem by dividing the ‘H...read more

Add answer anonymously...
NXP Semiconductors Software Engineer 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