Given two integers ‘N’ and ‘K’, the task is to count the number of triplets (a, b, c) of positive integers not greater than ‘N’ such that ‘a+b’, ‘b+c’ and ‘c+a’ are all multiples of ‘K’. Note that ‘a’, ‘b’ and ‘c’ may or may not be the same in a triplet pair.
Input: N = 2, K = 2
All possible triplets are
(1, 1, 1) and (2, 2, 2)
Input: N = 3, K = 2
Approach: Run three nested loops from ‘1’ to ‘N’ and check whether
i+j, j+l and l+i are all divisible by ‘K’. Increment the count if the condition is true.
Below is the implementation of the above approach:
- Subset with no pair sum divisible by K
- Minimum pair sum operations to make array each element divisible by 4
- Probability of a random pair being the maximum weighted pair
- Pair with given product | Set 1 (Find if any pair exists)
- Pair formation such that maximum pair sum is minimized
- Find a triplet that sum to a given value
- Count the number of pairs (i, j) such that either arr[i] is divisible by arr[j] or arr[j] is divisible by arr[i]
- Split an array into groups of 3 such that X3 is divisible by X2 and X2 is divisible by X1
- Maximum triplet sum in array
- Pythagorean Triplet in an array
- Find a triplet such that sum of two equals to third element
- Largest triplet product in a stream
- Smallest Difference Triplet from Three arrays
- Find a triplet in an array whose sum is closest to a given number
- Maximum product of a triplet (subsequnece of size 3) in array
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.