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
- 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
- Count of Numbers in Range where the number does not contain more than K non zero digits
- Number of integers in a range [L, R] which are divisible by exactly K of it's digits
- Find the length of the Largest subset such that all elements are Pairwise Coprime
- Length of the longest increasing subsequence such that no two adjacent elements are coprime
- Coprime divisors of a number
- Largest number less than or equal to N/2 which is coprime to N
- 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
- Maximum sum of even indexed elements obtained by right shift on an even sized subarray
- Find maximum points which can be obtained by deleting elements from array
- Count of elements having odd number of divisors in index range [L, R] for Q queries
- Number of distinct integers obtained by lcm(X, N)/X
- Find the final number obtained after performing the given operation
- Number of ways to select equal sized subarrays from two arrays having atleast K equal pairs of elements
- 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
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.