Subarray Sums I Problem Statement
You are provided with an array of positive integers ARR
that represents the strengths of different “jutsus” (ninja techniques). You are also given the strength of the enemy S
, which is a positive integer.
Your task is to count the number of subarrays whose combined strength matches the given enemy strength S
.
Example:
Input:
ARR = [3, 1, 2, 4], S = 6
Output:
2
Explanation:
The subarrays from ARR
that sum up to the strength 6
are ARR[0...2]
and ARR[2...3]
.
Constraints:
1 <= T <= 100
1 <= N <= 10^3
1 <= ARR[i] <= 10^9
1 <= S <= 10^9
- Where
ARR[i]
is thei
th element of arrayARR
.
Note:
You do not need to print anything; it has already been taken care of. Just implement the given function.
Input Format:
The first line of input contains a single integer T
denoting the number of test cases. Then, for each test case: The first line contains two space-separated integers: N
(length of the ARR
) and S
(strength of the enemy). The next line contains N
space-separated integers, representing the elements of ARR
.
Output Format:
For each test case, return the number of subarrays that sum up to the given strength S
of the enemy. Output for each test case should be in a separate line.
AnswerBot
1d
Count the number of subarrays whose combined strength matches the given enemy strength.
Iterate through all subarrays and calculate their sum to check if it matches the enemy strength.
Use two pointers ...read more
Help your peers!
Add answer anonymously...
Top SAP Software Developer Intern interview questions & answers
Popular interview questions of Software Developer Intern
Stay ahead in your career. Get AmbitionBox app
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