FastPrepCount Decreasing Triplets

Count Decreasing Triplets

Airbnb logoAirbnb● MediumFULLTIMEOA
Learn

Problem statement

Given an integer array nums, count the index triplets (i, j, k) such that i < j < k and nums[i] > nums[j] > nums[k].

Return the number of strictly decreasing subsequences of length three. Equal values do not satisfy either strict inequality.

Function

countDecreasingTriplets(nums: int[]) → long

Examples

Example 1

nums = [9,4,6,3,2]return = 7

The valid value triples are (9,4,3), (9,4,2), (9,6,3), (9,6,2), (9,3,2), (4,3,2), and (6,3,2).

Example 2

nums = [5,4,3,2]return = 4

Every choice of three indices is strictly decreasing, so the answer is C(4,3) = 4.

Example 3

nums = [3,3,2,1]return = 2

Either occurrence of 3 can precede 2,1. The two equal 3s cannot form a strict pair with each other.

Constraints

  • 0 <= nums.length <= 200000.
  • -10^9 <= nums[i] <= 10^9.
  • The answer fits in a signed 64-bit integer.

More Airbnb problems

See Airbnb hiring insights
public long countDecreasingTriplets(int[] nums) {
    // Write your code here.
}
nums[9,4,6,3,2]
expected7
Checking account…