1338. Rising Triple Count

A trading analyst scans the price list nums for steady climbs. A rising triple is a set of indices i < j < k with nums[i] < nums[j] < nums[k], both comparisons strict. Return the number of rising triples as a 64-bit integer. For each middle index j, count the strictly smaller values to its left and the strictly larger values to its right, then add up the products of the two counts. Both counts can be gathered with a merge sort (or Fenwick trees), so the whole task takes O(n log n) time with O(n) extra space.

Example 1

Input:
nums = [1,2,3,4]
Output:
4
Explanation:

Any three of the four increasing values form a rising triple, which gives 4 triples.

Example 2

Input:
nums = [3,1,4,2,6,5]
Output:
6
Explanation:

Six triples rise: 3-4-6, 3-4-5, 1-4-6, 1-4-5, 1-2-6 and 1-2-5.

Example 3

Input:
nums = [3,3,3]
Output:
0
Explanation:

Equal values are never strictly increasing, so there are no triples.

Constraints

1 ≤ n ≤ 105

-109 ≤ nums[i] ≤ 109

How this problem is judged

Answers
Your answer must match exactly. Numbers compare by value, so 2 and 2.0 are equal.
Time per case
Python 2,000 msC++ 500 msJava 1,000 msJavaScript 1,000 msTypeScript 1,000 ms

Expected complexity

Time
O(n log n)
Space
O(n)

What the author was aiming for. Your own solution is not measured against it.

Asked in an interview

Were you asked this in an interview? Say where, anonymously.

Code
Loading the editor…