Description
Solutions
Submission
Get Triple Count
🔥 FULLTIME

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 dividable by d.

The goal is to find the number of distinct triplets (i, j, k) such that i < j < k and the sum (a[i] + a[j] + a[k]) is divisible by d.

Function Description

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

getTripleCount has the following parameters:

  • arr[]: an array of integers
  • d: an integer
  • Example 1:

    Input:  arr = [3, 3, 4, 7, 8], d = 5
    Output: 3
    Explanation:
    Let arr, prices of stock = [3, 3, 4, 7, 8] and d = 5. Following are the triplets whose sum is divisible by d (1-based indexing): a. Triplet with indices - (1, 2, 3), sum = 3 + 3 + 4 which is equal to 10 b. Triplet with indices - (1, 3, 5), sum = 3 + 4 + 8 which is equal to 15 a. Triplet with indices - (2, 3, 4), sum = 3 + 4 + 8 which is equal to 15 Hence the answer is 3.
    Constraints:
    • 3 <= n <= 103
    • 1 <= arr[i] <= 109
    • 2 <= d <= 106
    Testcase

    Result
    Case 1

    input:

    output: