Talentd
FleetCode

4049. Count Values With Equally Spaced Occurrences II

Medium62.9% acceptance

You are given an integer array nums.

An integer x is called special if:

  • x appears at least three times in nums.
  • All occurrences of x are equally spaced in nums. In other words, if all occurrences of x are at indices i1 < i2 < ... < im, then i2 - i1 = i3 - i2 = ... = im - im-1.

Return the number of distinct special integers in nums.

Example 1:

Input: nums = [1,8,1,5,1,5,8,5]

Output: 2

Explanation:

  • 1 is special because it occurs at equally spaced indices 0, 2, and 4.
  • 5 is special because it occurs at equally spaced indices 3, 5, and 7.
  • 8 is not special because it occurs only twice.

Therefore, the answer is 2.

Example 2:

Input: nums = [8,8,8,8]

Output: 1

Explanation:

8 is special because it occurs at equally spaced indices 0, 1, 2, and 3. Therefore, the answer is 1.

Example 3:

Input: nums = [8,6,6,8,8]

Output: 0

Explanation:

8 occurs at indices 0, 3, and 4, which are not equally spaced. 6 occurs only twice. Therefore, no integer is special.

Constraints:

  • 3 <= nums.length <= 105
  • 1 <= nums[i] <= 109

Hints open one at a time

Hint 1

Use a hash map to group occurrence indices by value. A value is special if its group contains at least three indices and every consecutive gap equals the first gap.

8:05
Still stuck?Read the written solution, or watch a walkthrough.
Open solution
Loading editor…
1class Solution:
2 def countSpecialIntegers(self, nums: list[int]) -> int:
3
Starter codePython 3Spaces: 4
Examples
Input
[1,8,1,5,1,5,8,5]
Input
[8,8,8,8]
Input
[8,6,6,8,8]
Your code is saved on this device.Solution