Related Articles

# Count of pairs in given range having their ratio equal to ratio of product of their digits

• Difficulty Level : Medium
• Last Updated : 29 Sep, 2021

Given two integers L and R, the task is to find the count of unordered pairs of integers (A, B) in the range [L, R] such that the ratio of A and B is the same as the ratio of the product of digits of A and the product of digits of B.

Examples:

Input: L = 10, R = 50
Output: 2
Explanation: The pairs in the range [10, 50] that follow the given condition are (15, 24) as 15 : 24 = 5 : 8 ≡ (1*5) : (2*4) = 5 : 8 and (18, 45) as 18 : 45 = 2 : 5 ≡ (1*8) : (4*5) = 8 : 20 = 2 : 5.

Input: L = 1, R = 100
Output: 43

Approach: The given problem can be solved using the steps discussed below:

• Create a function to calculate the product of digits of a number.
• Iterate over all the unordered pairs of integers in the range [L, R] using a and b for each pair (a, b), a : b is equivalent to the product of digits of a : product of digits of b if and only if a * product of digits of b = b * product of digits of a.
• Using the above observation, maintain the count of the valid pairs of (a, b) in a variable cntPair such that a * product of digits of b = b * product of digits of a.
• After completing the above steps, print the value of cntPair as the result.

Below is the implementation of the above approach:

## C++

 `// C++ program for the above approach` `#include ``using` `namespace` `std;` `// Function to find the product of``// digits of the given number``int` `getProduct(``int` `n)``{``    ``int` `product = 1;``    ``while` `(n != 0) {``        ``product = product * (n % 10);``        ``n = n / 10;``    ``}``    ``return` `product;``}` `// Function to find the count of pairs``// (a, b) such that a:b = (product of``// digits of a):(product of digits of b)``int` `countPairs(``int` `L, ``int` `R)``{``    ``// Stores the count of the valid pairs``    ``int` `cntPair = 0;` `    ``// Loop to iterate over all unordered``    ``// pairs (a, b)``    ``for` `(``int` `a = L; a <= R; a++) {``        ``for` `(``int` `b = a + 1; b <= R; b++) {` `            ``// Stores the product of``            ``// digits of a``            ``int` `x = getProduct(a);` `            ``// Stores the product of``            ``// digits of b``            ``int` `y = getProduct(b);` `            ``// If x!=0 and y!=0 and a:b``            ``// is equivalent to x:y``            ``if` `(x && y && (a * y) == (b * x)) {` `                ``// Increment valid pair count``                ``cntPair++;``            ``}``        ``}``    ``}` `    ``// Return Answer``    ``return` `cntPair;``}` `// Driver code``int` `main()``{``    ``int` `L = 1;``    ``int` `R = 100;` `    ``// Function Call``    ``cout << countPairs(1, 100);``    ``return` `0;``}`

## Java

 `// Java program for the above approach``class` `GFG{` `// Function to find the product of``// digits of the given number``public` `static` `int` `getProduct(``int` `n)``{``    ``int` `product = ``1``;``    ``while` `(n != ``0``) {``        ``product = product * (n % ``10``);``        ``n = n / ``10``;``    ``}``    ``return` `product;``}` `// Function to find the count of pairs``// (a, b) such that a:b = (product of``// digits of a):(product of digits of b)``public` `static` `int` `countPairs(``int` `L, ``int` `R)``{``    ``// Stores the count of the valid pairs``    ``int` `cntPair = ``0``;` `    ``// Loop to iterate over all unordered``    ``// pairs (a, b)``    ``for` `(``int` `a = L; a <= R; a++) {``        ``for` `(``int` `b = a + ``1``; b <= R; b++) {` `            ``// Stores the product of``            ``// digits of a``            ``int` `x = getProduct(a);` `            ``// Stores the product of``            ``// digits of b``            ``int` `y = getProduct(b);` `            ``// If x!=0 and y!=0 and a:b``            ``// is equivalent to x:y``            ``if` `(x !=``0`  `&& y != ``0` `&& (a * y) == (b * x)) {` `                ``// Increment valid pair count``                ``cntPair++;``            ``}``        ``}``    ``}` `    ``// Return Answer``    ``return` `cntPair;``}` `// Driver code``public` `static` `void`  `main(String args[])``{``    ``int` `L = ``1``;``    ``int` `R = ``100``;` `    ``// Function Call``    ``System.out.println(countPairs(L, R));``}` `}` `// This code is contributed by _saurabh_jaiswal.`

## Python3

 `# Python 3 program for the above approach` `# Function to find the product of``# digits of the given number``def` `getProduct(n):``    ``product ``=` `1``    ``while` `(n !``=` `0``):``        ``product ``=` `product ``*` `(n ``%` `10``)``        ``n ``=` `n ``/``/` `10``    ``return` `product` `# Function to find the count of pairs``# (a, b) such that a:b = (product of``# digits of a):(product of digits of b)``def` `countPairs(L, R):``    ``# Stores the count of the valid pairs``    ``cntPair ``=` `0` `    ``# Loop to iterate over all unordered``    ``# pairs (a, b)``    ``for` `a ``in` `range``(L,R``+``1``,``1``):``        ``for` `b ``in` `range``(a ``+` `1``,R``+``1``,``1``):``            ``# Stores the product of``            ``# digits of a``            ``x ``=` `getProduct(a)` `            ``# Stores the product of``            ``# digits of b``            ``y ``=` `getProduct(b)` `            ``# If x!=0 and y!=0 and a:b``            ``# is equivalent to x:y``            ``if` `(x ``and` `y ``and` `(a ``*` `y) ``=``=` `(b ``*` `x)):``                ``# Increment valid pair count``                ``cntPair ``+``=` `1` `    ``# Return Answer``    ``return` `cntPair` `# Driver code``if` `__name__ ``=``=` `'__main__'``:``    ``L ``=` `1``    ``R ``=` `100` `    ``# Function Call``    ``print``(countPairs(``1``, ``100``))``    ` `    ``# This code is contributed by SURENDRA_GANGWAR.`

## C#

 `// C# program for the above approach``using` `System;` `public` `class` `GFG{` `    ``// Function to find the product of``    ``// digits of the given number``    ``public` `static` `int` `getProduct(``int` `n)``    ``{``        ``int` `product = 1;``        ``while` `(n != 0) {``            ``product = product * (n % 10);``            ``n = n / 10;``        ``}``        ``return` `product;``    ``}` `    ``// Function to find the count of pairs``    ``// (a, b) such that a:b = (product of``    ``// digits of a):(product of digits of b)``    ``public` `static` `int` `countPairs(``int` `L, ``int` `R)``    ``{``        ``// Stores the count of the valid pairs``        ``int` `cntPair = 0;``    ` `        ``// Loop to iterate over all unordered``        ``// pairs (a, b)``        ``for` `(``int` `a = L; a <= R; a++) {``            ``for` `(``int` `b = a + 1; b <= R; b++) {``    ` `                ``// Stores the product of``                ``// digits of a``                ``int` `x = getProduct(a);``    ` `                ``// Stores the product of``                ``// digits of b``                ``int` `y = getProduct(b);``    ` `                ``// If x!=0 and y!=0 and a:b``                ``// is equivalent to x:y``                ``if` `(x !=0  && y != 0 && (a * y) == (b * x)) {``    ` `                    ``// Increment valid pair count``                    ``cntPair++;``                ``}``            ``}``        ``}``    ` `        ``// Return Answer``        ``return` `cntPair;``    ``}` `    ``// Driver code``    ``public` `static` `void` `Main(``string` `[]args)``    ``{``        ``int` `L = 1;``        ``int` `R = 100;``    ` `        ``// Function Call``        ``Console.WriteLine(countPairs(L, R));``    ``}` `}` `// This code is contributed by AnkThon`

## Javascript

 ``
Output:
`43`

Time Complexity: O(N2*log N) where N represents the number of integers in the given range i.e, R – L.
Auxiliary Space: O(1)

Attention reader! Don’t stop learning now. Get hold of all the important mathematical concepts for competitive programming with the Essential Maths for CP Course at a student-friendly price. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

My Personal Notes arrow_drop_up