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 the ith element of array ARR.
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
4mo

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!
Select
Add answer anonymously...

SAP Software Developer Intern interview questions & answers

A Software Developer Intern was asked 9mo agoQ. How would you convert a byte stream to a human-readable format without using ext...read more
A Software Developer Intern was asked 9mo agoQ. Given coins with denominations 1, 2, and 5, what is the minimum number of coins ...read more
A Software Developer Intern was asked 9mo agoQ. How do you find the number of nodes in a tree, and what is the time complexity o...read more

Popular interview questions of Software Developer Intern

A Software Developer Intern was asked 9mo agoQ1. How would you convert a byte stream to a human-readable format without using ext...read more
A Software Developer Intern was asked 9mo agoQ2. Given coins with denominations 1, 2, and 5, what is the minimum number of coins ...read more
A Software Developer Intern was asked 9mo agoQ3. How do you find the number of nodes in a tree, and what is the time complexity o...read more
SAP Software Developer Intern Interview Questions
Stay ahead in your career. Get AmbitionBox app
play-icon
play-icon
qr-code
Trusted by over 1.5 Crore job seekers to find their right fit company
80 L+

Reviews

10L+

Interviews

4 Cr+

Salaries

1.5 Cr+

Users

Contribute to help millions

Made with ❤️ in India. Trademarks belong to their respective owners. All rights reserved © 2025 Info Edge (India) Ltd.

Follow Us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter
Profile Image
Hello, Guest
AmbitionBox Employee Choice Awards 2025
Winners announced!
awards-icon
Contribute to help millions!
Write a review
Write a review
Share interview
Share interview
Contribute salary
Contribute salary
Add office photos
Add office photos
Add office benefits
Add office benefits