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:
- Count of numbers between range having only non-zero digits whose sum of digits is N and number is divisible by M
- Finding a Non Transitive Coprime Triplet in a Range
- Length of the longest increasing subsequence such that no two adjacent elements are coprime
- Count of Numbers in Range where the number does not contain more than K non zero digits
- Largest number less than or equal to N/2 which is coprime to N
- Partition first N natural number into two sets such that their sum is not coprime
- Count numbers in given range such that sum of even digits is greater than sum of odd digits
- 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
- Count Numbers in Range with difference between Sum of digits at even and odd positions as Prime
- GCD of elements in a given range
- Queries for GCD of all numbers of an array except elements in a given range
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.