Previous Next
0 / 21
Module DEMO-32-106DEMO-C-32

Subarray with Given Sum

DEMO-C-32 โ€ข Programming in C

Browser-only practice

Problem Statement

Given an unsorted array of non-negative integers, find a continuous subarray which adds to a given number S. If found, print the 1-based start and end indices. If multiple exist, print the first one found. If none, print -1.

Example

Input:

5 12 1 2 3 7 5

Output:

2 4

(2+3+7 = 12. Indices 2 to 4)

Constraints

n <= 100

Input Format

n, S, array

Output Format

Indices or -1

Sample Testcases

Submit runs every public testcase in this browser. Results and code never leave this device.

Sample #1
Public sample
5 12
1 2 3 7 5
2 4
2+3+7 = 12.
Sample #2
Public sample
4 0
0 0 0 0
1 1
First zero matches sum 0.
Sample #3
Public sample
6 9
1 2 3 4 5 6
2 4
Subarray [2,3,4] sums to 9.
Sample #4
Public sample
1 7
7
1 1
Single element matches the target sum.
Sample #5
Public sample
5 100
10 20 30 40 50
1 4
No subarray sums to 100.

Web terminal

C, C++, Java, and Python run locally in a browser VM. No worker or visualizer is used.

Saved in this browser
Editor settings