Language: C++ 20
Environment: Autocomplete Ready
#include <bits/stdc++.h>
using namespace std;
int getTripletCount(vector<int> arr, int d) {
// Write your code here
}
int main() {
// Test cases
}
Question 1:
Find all 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, k such that i < j < k and the sum a[i] + a[j] + a[k] is divisible by d.
Example 1:
Let arr be the prices of stock = [3, 3, 4, 7, 8] and d = 5. Following are the triplets whose sum is divisible by d (1-based indexing):
- Triplet with indices 1, 2, 3, sum = 3 + 3 + 4 which is equal to 10
- Triplet with indices 1, 3, 5, sum = 3 + 4 + 8 which is equal to 15
- Triplet with indices 2, 3, 4, sum = 3 + 4 + 8 which is equal to 15
Hence the answer is 3.
Function Description:
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:
- arr: an array of integers
- d: an integer
Constraints:
3 <= n <= 10^3
1 <= arr[i] <= 10^9
Test Results:
Custom Input: DELL