FastPrepGet Triplet Count

Get Triplet Count

IBM logoIBM● MediumFULLTIMEOA
Learn

Problem statement

The cost of a stock on each day is given in an integer array arr. Count the number of distinct index triplets (i, j, k) such that i < j < k and (arr[i] + arr[j] + arr[k]) is divisible by d.

Return the total number of qualifying triplets. Equal values at different indices represent distinct choices.

Function

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

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.

Constraints

  • 3 <= arr.length <= 2000.
  • -10^9 <= arr[i] <= 10^9.
  • 1 <= d <= 10^9.
  • The answer fits in a signed 32-bit integer.

More IBM problems

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