UKG Coding Question β Solved
There is an integer array arr[n] and an integer value d. The array is indexed from 1 to n. Count the number of distinct triplets (i, j, k) such that 0 < i < j < k β€ n and the sum (a[i] + a[j] + a[k]) is divisible by d.
Example: a = [3, 3, 4, 7, 8], d = 5. The following triplets are divisible by d = 5. These are the triplets whose sum is divisible by d (1-based indexing):
Β· indices (1, 2, 3), sum = 3 + 3 + 4 = 10
Β· indices (1, 3, 5), sum = 3 + 4 + 8 = 15
Β· indices (2, 3, 4), sum = 3 + 4 + 8 = 15
Since there is no other triplet divisible by d = 5, return 3.
Function Description: Complete the function getTripletCount in the editor below.
getTripletCount has the following parameters:
int a[n]: an array of integers
int d: an integer
Returns: int β the number of distinct triplets
Constraints:
Β· 1 β€ a[i] β€ 10^9
Β· 2 β€ d β€ 10^9
Input Format For Custom Testing:
Sample Case 0
Sample Input For Custom Testing:
STDIN
2
FUNCTION
4
a = [2, 3, 1, 6]
1
6
3
Sample Output:
3