#C7873. Sum Between Occurrences
Sum Between Occurrences
Sum Between Occurrences
Given an array of integers and a target integer x, compute the sum of the elements between the first and last occurrence of x (both inclusive). If x does not appear at least twice, then the result should be 0.
The task is to implement a function that reads its input from standard input (stdin) and writes the answer to standard output (stdout). In other words, you need to find the first index i and the last index j (with i ≤ j) such that a[i]=a[j]=x, and then output the sum \(\sum_{k=i}^{j} a[k]\). If x appears only once or not at all, output 0.
Note: Use LaTeX format for any formulas, e.g., the sum formula is given by \(\sum_{k=i}^{j} a[k]\).
inputFormat
The input is read from stdin and consists of three lines:
- The first line contains an integer \(n\), the number of elements in the array.
- The second line contains \(n\) space-separated integers representing the array elements.
- The third line contains an integer \(x\), the target number.
outputFormat
Print a single integer to stdout which is the sum of the elements between the first and last occurrence of \(x\) (inclusive). If \(x\) appears fewer than twice in the array, print 0.
## sample7
1 3 5 2 3 7 8
3
13