Given two numbers A and B where 1 <= A <= B. The task is to count the number of pairs whose elements are co-prime where pairs are formed from the sum of the digits of the elements in the given range.
Note: Two pairs are counted as distinct if at least one of the number in the pair is different. It may be assumed that the maximum digit sum can be 162.
Input: 12 15 Output: 4 12 = 1+2 = 3 13 = 1+3 = 4 14 = 1+4 = 5 15 = 1+5 = 6 Thus pairs who are co-prime to each other are (3, 4), (3, 5), (4, 5), (5, 6) i.e the answer is 4. Input: 7 10 Output: 5
- Consider each and every element from a to b.
- Find the sum of the digits of every element and store it into a vector.
- Consider each and every pair one by one and check if the gcd of the elements of that pair is 1.
- If yes, count that pair as it is co-prime.
- Print the count of pairs that are co-prime.
Below is the implementation of above approach:
As mentioned in the question, the maximum sum can be 162. So, find out the frequency of numbers having their digit sum from 1 to 162 in range A to B and store the frequency in the array. Later, find the answer using this frequency.
Thus Number of gcd pairs = freq(3)*freq(4) + freq(3)*freq(5) + freq(4)*freq(5) + freq(5)* freq(6)
Thus pairs who are co-prime to each other are (3,4), (3,5), (4,5), (5,6) i.e the answer is 4.
Below is the required implementation:
- Finding a Non Transitive Coprime Triplet in a Range
- Find maximum points which can be obtained by deleting elements from array
- Number of distinct integers obtained by lcm(X, N)/X
- Pairs with GCD equal to one in the given range
- All possible co-prime distinct element pairs within a range [L, R]
- Smallest number with given sum of digits and sum of square of digits
- GCD of elements in a given range
- Queries for GCD of all numbers of an array except elements in a given range
- Number which has the maximum number of distinct prime factors in the range M to N
- Count number of pairs (A <= N, B <= N) such that gcd (A , B) is B
- Given GCD G and LCM L, find number of possible pairs (a, b)
- Number of ordered pairs such that (Ai & Aj) = 0
- Number of prime pairs in an array
- Count pairs (A, B) such that A has X and B has Y number of set bits and A+B = C
- Number of co-prime pairs in an 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 email@example.com. 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.