Question#1
Given an array of n distinct integers, d = [d[0], d[1], …, d[n−1]], and an integer threshold, t, determine the number of (a, b, c) index triplets that satisfy both the conditions:
d[a] < d[b] < d[c]
d[a] + d[b] + d[c] ≤ t
Example
d = [1,2,3,4,5]
t = 8
The following 4 triplets satisfy the constraints:
(1,2,3) → 1 + 2 + 3 = 6 ≤ 8
(1,2,4) → 1 + 2 + 4 = 7 ≤ 8
(1,2,5) → 1 + 2 + 5 = 8 ≤ 8
(1,3,4) → 1 + 3 + 4 = 8 ≤ 8
Function Description
Complete the function triplets in the editor below.
triplets has the following parameter(s):
int t: the threshold
int d[n]: an array of integers
Returns:
long: a long integer that denotes the number of (a, b, c) triplets that sastisfy the given conditions
Constraints
1 ≤ n ≤ 104
0 ≤ d[i] < 109
0 < t < 3 × 109
Question#2
Contiguous subarrays are a group of an uninterrupted range of elements from an array as they occur. No elements in the range can be skipped or reordered. Given an array of integers, numbers, and an integer k, determine the total number of subarrays of numbers that have a product that is less than or equal to k.
Example
numbers = [2, 3, 4]
The subarrays are [[2], [3], [4], [2, 3], [3, 4], [2, 3, 4]]. The product of a subarray is the product of all of its elements so the products for the list of subarrays are [2, 3, 4, 6, 12, 24]. If k = 6, 4 subarrays satisfy the condition, [2], [3], [4], [2, 3].
Function Description
Complete the function count in the editor below.
count has the following parameter(s):
int numbers[n]: an array of integers
k: an integer
Returns
long int: the number of subarrays whose product is less than or equal to k
Constraints
1 ≤ n ≤ 5 × 105
1 ≤ numbers[i] ≤ 100
1 ≤ k ≤ 106


