#K86527. Contiguous Books Sum
Contiguous Books Sum
Contiguous Books Sum
You are given a sequence of n books arranged on a shelf, where each book has a thickness. Your task is to count the number of ways to select three contiguous books such that their total thickness is exactly S.
More formally, given an integer \( n \) representing the total number of books, an integer \( S \) for the desired sum, and an array \( thicknesses \) where \( thicknesses[i] \) denotes the thickness of the \( i^{th} \) book (with \( 0 \leq i < n \)), determine the number of starting positions \( i \) (with \( 0 \leq i \leq n-3 \)) such that:
\( thicknesses[i] + thicknesses[i+1] + thicknesses[i+2] = S \)
The input format and output format are provided below. Solve this problem by reading from standard input and writing to standard output.
inputFormat
The input consists of two lines:
- The first line contains two integers \( n \) and \( S \) separated by a space, where \( n \) is the number of books and \( S \) is the target sum.
- The second line contains \( n \) integers separated by spaces, representing the thicknesses of the books in order.
outputFormat
Output a single integer: the number of ways to choose three contiguous books such that their total thickness equals \( S \).
## sample5 6
1 2 3 2 1
2