FastPrepCount Good Tuples
Problem · Array

Count Good Tuples

Learn this problem
EasyCapital One logoCapital OneINTERNOA

Problem 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[]) → int

Examples

Example 1

a = [1,1,1,2,1,3,4]return = 2

The 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.

More Capital One problems

drafts saved locally
public int solution(int[] a) {
  // Write your code here.
}
a[1,1,1,2,1,3,4]
expected2
checking account