Problem · Array
Count Good Tuples
Learn this problemProblem statement
You are given an integer array a. A tuple of three consecutive values is good when exactly two of its three values are equal.
For example, (2, 1, 2) is good; (1, 1, 1) and (1, 2, 3) are not. Return the number of good tuples. Tuples may overlap.
A solution no worse than O(a.length²) is accepted.
Function
solution(a: int[]) → intExamples
Example 1
a = [1,1,1,2,1,3,4]return = 2The five triples are (1,1,1), (1,1,2), (1,2,1), (2,1,3), and (1,3,4). Only the second and third have exactly two equal values.
Constraints
0 ≤ a.length ≤ 100000-1000000000 ≤ a[i] ≤ 1000000000- An array with fewer than three elements contains no tuples of length three.