Happy and sum
Practice
5 (1 votes)
Combinatorics
Dynamic programming
Algorithms
Approved
Medium Hard
Problem
58% Success 392 Attempts 50 Points 2s Time Limit 256MB Memory 1024 KB Max Code

Once, happy and his friends were playing with a list of numbers. They take a subset of the numbers from the given list and add the numbers in the subset. As this was a very simpler task and happy loves counting, he decided to find the number of subsets from the given list that form a particular sum. Seeing this, his friend asks him to tell the number of subsets whose sum of elements lies in a given range. As this value can be very large, he asks happy to tell the answer modulo \(10^{9}\) + 7.

The list consists of N integers and his friend asks him M queries. In each query, he will give two numbers L and R to happy and he has to tell the number of subsets that has the sum of its elements in the range L to R ( both inclusive).

The subsets \(S1\) and \(S2\) are considered different if there exist at least one index i that belongs to \(S1\) but does not belong to \(S2\) where i \(\in\) [1, N].

Input:

The first line of the input consists of integers N and M.

The second line of the input contains N single space separated integers.
The next M lines of input consists of the integers L and R for which happy has to tell the answer.

Output:

Print the answer corresponding to each query in a new line.

Constraints:

  • \(1 ≤ N ≤ 10^{3}\)
  • \(1 ≤ M ≤ 5 * 10^{4}\)
  • \(1 ≤ A_{i} ≤ 10^{3}\)
  • \(1 ≤ L ≤ R ≤ 10^{6}\)

Please login to use the editor

You need to be logged in to access the code editor

Loading...

Please wait while we load the editor

Loading...
Results
Custom Input
Run your code to see the output
Submissions
Please login to view your submissions
Similar Problems
Points:50
3 votes
Tags:
Medium-Hard
Points:50
11 votes
Tags:
Medium-Hard
Points:50
6 votes
Tags:
Counting and ArrangementsDynamic ProgrammingAlgorithmsMedium-Hard