Count of pairs in a given range with sum of their product and sum equal to their concatenated number

Given two numbers A and B, the task is to find the count of pairs (X, Y) in range [A, B], such that (X * Y) + (X + Y) is equal to the number formed by concatenation of X and Y
Examples:Â
Â
Input: A = 1, B = 9Â
Output: 9Â
Explanation:Â
The pairs (1, 9), (2, 9), (3, 9), (4, 9), (5, 9), (6, 9), (7, 9), (8, 9) and (9, 9) are the required pairs.
Input: A = 4, B = 10Â
Output: 7Â
Explanation: The pairs (4, 9), (5, 9), (6, 9), (7, 9), (8, 9), (9, 9) and (10, 9) satisfy the required condition.Â
Â
Â
Approach :Â
We can observe that any number of the form [9, 99, 999, 9999, ….] satisfies the condition with all other values.Â
Â
Illustration:Â
If Y = 9, the required condition is satisfied for all values of X.Â
{1*9 + (1 + 9) = 19, 2*9 + (2 + 9) = 29, ……….. 11*9 + (11 + 9) = 119 …..Â
Similarly, for Y = 99, 1*99 + 1 + 99 = 199, 2*99 + 2 + 99 = 299, ………Â
Â
Hence, follow the steps below to solve the problems:Â
Â
- Count the number of possible values of Y of the form {9, 99, 999, 9999, ….} in range [A, B] and store in countY
- Count the number of possible values of X in the range [A, B] as countXÂ
Â
countX = (B - A + 1)
- Â
- The required count will be the product of possible count of X and Y, i.e.Â
Â
answer = countX * countY
- Â
Below is the implementation of the above approach:
Â
C++
// C++ program to count// all the possible pairs// with X*Y + (X + Y) equal to// number formed by// concatenating X and YÂ
#include <bits/stdc++.h>using namespace std;Â
// Function for counting pairsint countPairs(int A, int B){Â
    int countY = 0,        countX = (B - A) + 1,        next_val = 9;Â
    // Count possible values    // of Y    while (next_val <= B) {        if (next_val >= A) {            countY += 1;        }        next_val = next_val * 10 + 9;    }Â
    return (countX * countY);}Â
// Driver Codeint main(){Â Â Â Â int A = 1;Â Â Â Â int B = 16;Â Â Â Â cout << countPairs(A, B);Â Â Â Â return 0;} |
Java
// Java program to count// all the possible pairs// with X*Y + (X + Y) equal to// number formed by// concatenating X and Yimport java.util.*;class GFG{Â
// Function for counting pairsstatic int countPairs(int A, int B){    int countY = 0,        countX = (B - A) + 1,        next_val = 9;Â
    // Count possible values    // of Y    while (next_val <= B)     {        if (next_val >= A)         {            countY += 1;        }        next_val = next_val * 10 + 9;    }    return (countX * countY);}Â
// Driver Codepublic static void main(String args[]){Â Â Â Â int A = 1;Â Â Â Â int B = 16;Â Â Â Â System.out.print(countPairs(A, B));}}Â
// This code is contributed by Code_Mech |
Python3
# Python3 program to count# all the possible pairs# with X*Y + (X + Y) equal to# number formed by# concatenating X and YÂ
# Function for counting pairsdef countPairs(A, B):Â
    countY = 0    countX = (B - A) + 1    next_val = 9Â
    # Count possible values    # of Y    while (next_val <= B):        if (next_val >= A):            countY += 1        next_val = next_val * 10 + 9Â
    return (countX * countY)Â
# Driver Codeif __name__ == '__main__':Â Â Â Â Â Â Â Â Â A = 1Â Â Â Â B = 16Â Â Â Â Â Â Â Â Â print(countPairs(A, B))Â
# This code is contributed by mohit kumar 29 |
C#
// C# program to count// all the possible pairs// with X*Y + (X + Y) equal to// number formed by// concatenating X and Yusing System;class GFG{Â
// Function for counting pairsstatic int countPairs(int A, int B){    int countY = 0,        countX = (B - A) + 1,        next_val = 9;Â
    // Count possible values    // of Y    while (next_val <= B)     {        if (next_val >= A)         {            countY += 1;        }        next_val = next_val * 10 + 9;    }    return (countX * countY);}Â
// Driver Codepublic static void Main(){Â Â Â Â int A = 1;Â Â Â Â int B = 16;Â Â Â Â Console.Write(countPairs(A, B));}}Â
// This code is contributed by Akanksha_Rai |
Javascript
<script>// javascript program to count// all the possible pairs// with X*Y + (X + Y) equal to// number formed by// concatenating X and YÂ
    // Function for counting pairs    function countPairs(A , B)     {        var countY = 0, countX = (B - A) + 1, next_val = 9;Â
        // Count possible values        // of Y        while (next_val <= B)         {            if (next_val >= A)            {                countY += 1;            }            next_val = next_val * 10 + 9;        }        return (countX * countY);    }Â
    // Driver Code        var A = 1;        var B = 16;        document.write(countPairs(A, B));Â
// This code is contributed by todaysgaurav </script> |
16
Â
Time Complexity: O(log10(B))
Space Complexity: O(1)
Â
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 zambiatek!



