26 April 2024 — All Subarrays with Sum Equal to K

M · mediumP · Verified PYQAmbiguous I/Oarrays

Find and print all contiguous subarrays of a given array whose elements sum equals K. Note: Input format is unusual — integers separated by spaces, with a comma before K. Example input line: 1 2 3 4 5 -4 -3 ,10 (comma separates array from K) Print each qualifying subarray on a separate line.

Input
1 2 3 4 ,6
Output
1 2 3
2 4
6
The I/O format for this question is intentionally ambiguous — TCS NQT sometimes omits full specs. Practice parsing.
Parse input until non-integer char (comma). Use brute force O(N^2): for each i, sum from i to j until sum>=K.
Note: The ambiguous I/O format is intentional — TCS NQT sometimes has unclear input specs. Practice parsing!
← 14 May 2024 Shift 2 — Pattern: Right-Angle Triangle of Numbers26 April 2024 Shift 1 — Average Sales Calculation →
Report an issue with this question