Skip to main content

TCS

TCS OA-31 2024

Problem Description

You are given an array of integers, nums, and an integer k. Your task is to find and print all contiguous non-empty subarrays within nums whose sum equals k.  

Input Format

  • The first line contains an integer N, representing the array's length.
  • The second line contains N space-separated integers.
  • The third line contains an integer k.

Output Format

Print all contiguous subarrays whose sum equals k. If none exist, print "No subarrays found."

Constraints

  • 1 ≤ arr.size() ≤ 10⁴
  • -10³ ≤ arr[i] ≤ 10³
  • -10⁷ ≤ k ≤ 10⁷

Example

Sample Input:

3
[1,1,1]
2

Sample Output:

[[1,1],[1,1]]

Explanation:

There are 2 subarrays whose sum is 2 ie [1,1] and [1, 1].