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:
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.
- Count all pairs of divisors of a number N whose sum is coprime with N
- Finding a Non Transitive Coprime Triplet in a Range
- Count numbers in given range such that sum of even digits is greater than sum of odd digits
- Partition first N natural number into two sets such that their sum is not coprime
- Length of the longest increasing subsequence such that no two adjacent elements are coprime
- Find the length of the Largest subset such that all elements are Pairwise Coprime
- Largest number less than or equal to N/2 which is coprime to N
- Coprime divisors of a number
- Count of numbers between range having only non-zero digits whose sum of digits is N and number is divisible by M
- Smallest number with given sum of digits and sum of square of digits
- Largest Coprime Set Between two integers
- Print all distinct Coprime sets possible from 1 to N
- Count of integers up to N which are non divisors and non coprime with N
- Maximum sum of even indexed elements obtained by right shift on an even sized subarray
- Find the final number obtained after performing the given operation
- Find maximum points which can be obtained by deleting elements from array
- Maximum subsequence sum obtained by concatenating disjoint subarrays whose lengths are prime
- Numbers with a Fibonacci difference between Sum of digits at even and odd positions in a given range
- Count all prime numbers in a given range whose sum of digits is also prime
- Count of distinct sums that can be obtained by adding prime numbers from given arrays
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.