#C6071. Smallest C-Balanced Subarray

    ID: 49791 Type: Default 1000ms 256MiB

Smallest C-Balanced Subarray

Smallest C-Balanced Subarray

You are given an array of integers and a target integer c. Your task is to find the length of the smallest contiguous subarray whose elements sum up exactly to c. If no such subarray exists, output -1.

Definition: A subarray is called c-balanced if the sum of its elements is equal to c.

Example:

  • For input: n = 5, c = 7, arr = [1, 2, 3, 4, 5], one valid subarray is [3, 4] and the answer is 2.

This problem tests your ability to efficiently process arrays and use prefix sum techniques.

inputFormat

The input is read from standard input (stdin) and consists of two lines:

  • The first line contains two integers n and c where n is the number of elements in the array and c is the target sum.
  • The second line contains n space-separated integers representing the array elements.

outputFormat

Print a single integer representing the length of the smallest contiguous subarray whose sum equals c. If such a subarray does not exist, print -1.

## sample
5 7
1 2 3 4 5
2

</p>