Count Special Subsequences

You are given an array nums consisting of positive integers.

A special subsequence is defined as a subsequence of length 4, represented by indices (p, q, r, s), where p < q < r < s. This subsequence must satisfy the following conditions:

  • nums[p] * nums[r] == nums[q] * nums[s]
  • There must be at least one element between each pair of indices. In other words, q - p > 1, r - q > 1, and s - r > 1.

Return the number of different special subsequences in nums.

Example 1
Inputnums = [1,2,3,4,3,6,1]
Output1
The only special subsequence is at indices (0, 2, 4, 6), where the products are both 3.
Example 2
Inputnums = [3,4,3,4,3,4,3,4]
Output3
There are three valid index quadruples: (0, 2, 4, 6), (1, 3, 5, 7), and (0, 2, 5, 7).

Constraints

  • 7 <= nums.length <= 1000
  • 1 <= nums[i] <= 1000

Asked at 2 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate