Count of pairs in given range having their ratio equal to ratio of product of their digits
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++
#include <bits/stdc++.h>
using namespace std;
int getProduct( int n)
{
int product = 1;
while (n != 0) {
product = product * (n % 10);
n = n / 10;
}
return product;
}
int countPairs( int L, int R)
{
int cntPair = 0;
for ( int a = L; a <= R; a++) {
for ( int b = a + 1; b <= R; b++) {
int x = getProduct(a);
int y = getProduct(b);
if (x && y && (a * y) == (b * x)) {
cntPair++;
}
}
}
return cntPair;
}
int main()
{
int L = 1;
int R = 100;
cout << countPairs(1, 100);
return 0;
}
|
Java
class GFG{
public static int getProduct( int n)
{
int product = 1 ;
while (n != 0 ) {
product = product * (n % 10 );
n = n / 10 ;
}
return product;
}
public static int countPairs( int L, int R)
{
int cntPair = 0 ;
for ( int a = L; a <= R; a++) {
for ( int b = a + 1 ; b <= R; b++) {
int x = getProduct(a);
int y = getProduct(b);
if (x != 0 && y != 0 && (a * y) == (b * x)) {
cntPair++;
}
}
}
return cntPair;
}
public static void main(String args[])
{
int L = 1 ;
int R = 100 ;
System.out.println(countPairs(L, R));
}
}
|
Python3
def getProduct(n):
product = 1
while (n ! = 0 ):
product = product * (n % 10 )
n = n / / 10
return product
def countPairs(L, R):
cntPair = 0
for a in range (L,R + 1 , 1 ):
for b in range (a + 1 ,R + 1 , 1 ):
x = getProduct(a)
y = getProduct(b)
if (x and y and (a * y) = = (b * x)):
cntPair + = 1
return cntPair
if __name__ = = '__main__' :
L = 1
R = 100
print (countPairs( 1 , 100 ))
|
C#
using System;
public class GFG{
public static int getProduct( int n)
{
int product = 1;
while (n != 0) {
product = product * (n % 10);
n = n / 10;
}
return product;
}
public static int countPairs( int L, int R)
{
int cntPair = 0;
for ( int a = L; a <= R; a++) {
for ( int b = a + 1; b <= R; b++) {
int x = getProduct(a);
int y = getProduct(b);
if (x !=0 && y != 0 && (a * y) == (b * x)) {
cntPair++;
}
}
}
return cntPair;
}
public static void Main( string []args)
{
int L = 1;
int R = 100;
Console.WriteLine(countPairs(L, R));
}
}
|
Javascript
<script>
function getProduct(n) {
let product = 1;
while (n != 0) {
product = product * (n % 10);
n = Math.floor(n / 10);
}
return product;
}
function countPairs(L, R) {
let cntPair = 0;
for (let a = L; a <= R; a++) {
for (let b = a + 1; b <= R; b++) {
let x = getProduct(a);
let y = getProduct(b);
if (x && y && (a * y) == (b * x)) {
cntPair++;
}
}
}
return cntPair;
}
let L = 1;
let R = 100;
document.write(countPairs(1, 100));
</script>
|
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)
Share your thoughts in the comments
Please Login to comment...