Consecutive Duplicates
MediumPractice library · +20 XParrayscountingrun-length encoding
Given an array of N integers, find the number of contiguous subarrays that consist of only a single distinct value (i.e., all elements in the subarray are identical).
Input
The first line contains an integer N. The second line contains N space-separated integers, the elements of the array.
Output
Print a single integer representing the count of such subarrays.
Constraints
1 <= N <= 200000. The elements of the array are between -10^9 and 10^9.
Time limit 1 s (native code; slower languages get more)Memory 256 MB14 hidden tests
Example 1
Input
5 1 1 2 2 2
Output
9
The subarrays are [1], [1], [1,1], [2], [2], [2], [2,2], [2,2], [2,2,2]. Total 9.
Example 2
Input
3 1 2 3
Output
3
The subarrays are [1], [2], [3]. Total 3.
Hints
# Read the input from stdin and print the answer.
import sys
def main():
data = sys.stdin.read().split()
# your code here
main()
Example 1 input
5 1 1 2 2 2
Example 2 input
3 1 2 3