
Suppose the given 'ARR' is [ 2, -1, 3, 5, 2 ], and the value 'K' is given to ninja is ‘7’.
So we return ‘10’ as the answer. i.e. ‘ARR’[0], ‘ARR’[1], ‘ARR’[2], ‘ARR’[3], ‘ARR’[4], ‘ARR’[0...1], ‘ARR’[0...2], ‘ARR’[1...2], ‘ARR’[1...3], ‘ARR’[3...4].
You are not required to print anything explicitly. It has already been taken care of. Just implement the function.
The first line of input contains a ‘T’ number of test cases. Then ‘T’ line follows:
The first line of each test case contains two integers input ‘N’ and ‘K’ i.e size of the array 'ARR' and the given value of the sum.
The second line of each test case contains 'N' single-spaced integers denoting the elements of 'ARR'.
For each test case, return the total number of possible combinations.
1 <= T <= 500
1 <= N <= 1000
-10 ^ 3 <= ARR[i], K <=10 ^ 3
Where ‘ARR[i]’ represents the value of the 'ith' item.
Time Limit: 1 second
The idea here is to traverse all the subarrays and calculate their sum. If the sum is equal or less than the required sum then increment the count of subarrays.
Algorithm
The idea here is to use the multiset and store the value of the current sum in it. So whenever we find there is a difference between the current sum value and the given sum we check values less than already present in our multiset and increment the final answer by that count.
Algorithm
The idea here is to use the concept of ‘Fenwick Tree’. So whenever we find that there is a difference between the current sum value and the given sum we, with the help of ‘Fenwick Tree’, find the count of those elements and add them in the final answer.
Algorithm