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:
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Pair formation such that maximum pair sum is minimized
- Number of indices pair such that element pair sum from first Array is greater than second Array
- Split an array into groups of 3 such that X3 is divisible by X2 and X2 is divisible by X1
- Find a triplet in an array such that arr[i] arr[k] and i < j < k
- 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]
- Find a triplet such that sum of two equals to third element
- Check whether there exists a triplet (i, j, k) such that arr[i] < arr[k] < arr[j] for i < j < k
- Store duplicate keys-values pair and sort the key-value pair by key
- Pair with given product | Set 1 (Find if any pair exists)
- Probability of a random pair being the maximum weighted pair
- Print all the combinations of N elements by changing sign such that their sum is divisible by M
- Find an array element such that all elements are divisible by it
- Subsequence pair from given Array having all unique and all same elements respectively
- Length of array pair formed where one contains all distinct elements and other all same elements
- Subset with no pair sum divisible by K
- Minimum pair sum operations to make array each element divisible by 4
- Count of subarrays of size K having at least one pair with absolute difference divisible by K-1
- Check if any permutation of array contains sum of every adjacent pair not divisible by 3
- Pythagorean Triplet in an array
- Maximum product of a triplet (subsequence 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.