FastPrepGet Triplet Count

Get Triplet Count

Point72 logoPoint72● MediumNEW GRADINTERNOA
Learn

Problem statement

Same as LC.2964

The cost of a stock on each day is given in an array, arr. An investor wants to buy the stocks in triplets such that the sum of the cost for three days is divisible by d. The goal is to find the number of distinct triplets (i, j, k) such that i < j < k and the sum (arr[i]+arr[j]+arr[k]) is divisible by d.

Function

getTripletCount(arr: int[], d: int) → int

Complete the function getTripletCount in the editor below. The function must return an integer denoting the total number of distinct triplets.

getTripletCount has the following parameters:

  1. int arr[n]: array of integers
  2. int d: the divisor

Examples

Example 1

arr = [3, 3, 4, 7, 8]d = 5return = 3

The triplets whose sum is divisible by d are shown.

  • Triplet with indices - (0, 1, 2), sum = 3+3+4 = 10
  • Triplet with indices - (0, 2, 4), sum = 3+4+8 = 15
  • Triplet with indices - (1, 2, 4), sum = 3+4+8 = 15

Hence, the answer is 3.

More Point72 problems

See Point72 hiring insights
public int getTripletCount(int[] arr, int d) {
  // write your code here
}
arr[3, 3, 4, 7, 8]
d5
expected3
Checking account…