Given two integers A and B, the task is to calculate the number of pairs (a, b) such that 1 ? a ? A, 1 ? b ? B and the equation (a * b) + a + b = concat(a, b) is true where conc(a, b) is the concatenation of a and b (for example, conc(12, 23) = 1223, conc(100, 11) = 10011). Note that a and b should not contain any leading zeroes.
Examples:Â
Input: A = 1, B = 12Â
Output: 1Â
There exists only one pair (1, 9) satisfyingÂ
the equation ((1 * 9) + 1 + 9 = 19)Input: A = 2, B = 8Â
Output: 0Â
There doesn’t exist any pair satisfying the equation.Â
Â
Approach: It can be observed that the above (a * b + a + b = conc(a, b)) will only be satisfied when the digits of an integer ? b contains only 9. Simply, calculate the number of digits (? b) containing only 9 and multiply with the integer a.
Below is the implementation of the above approach:Â
C++
// C++ implementation of the approach#include <bits/stdc++.h>using namespace std;Â
// Function to return the number of// pairs satisfying the equationint countPair(int a, int b){    // Converting integer b to string    // by using to_string function    string s = to_string(b);Â
    // Loop to check if all the digits    // of b are 9 or not    int i;    for (i = 0; i < s.length(); i++) {Â
        // If '9' doesn't appear        // then break the loop        if (s[i] != '9')            break;    }Â
    int result;Â
    // If all the digits of b contain 9    // then multiply a with string length    // else multiply a with string length - 1    if (i == s.length())        result = a * s.length();    else        result = a * (s.length() - 1);Â
    // Return the number of pairs    return result;}Â
// Driver codeint main(){Â Â Â Â int a = 5, b = 101;Â
    cout << countPair(a, b);Â
    return 0;} |
Java
// Java implementation of the approachclass GFG{Â
// Function to return the number of// pairs satisfying the equationstatic int countPair(int a, int b){    // Converting integer b to String    // by using to_String function    String s = String.valueOf(b);Â
    // Loop to check if all the digits    // of b are 9 or not    int i;    for (i = 0; i < s.length(); i++)    {Â
        // If '9' doesn't appear        // then break the loop        if (s.charAt(i) != '9')            break;    }Â
    int result;Â
    // If all the digits of b contain 9    // then multiply a with String length    // else multiply a with String length - 1    if (i == s.length())        result = a * s.length();    else        result = a * (s.length() - 1);Â
    // Return the number of pairs    return result;}Â
// Driver codepublic static void main(String[] args){Â Â Â Â int a = 5, b = 101;Â
    System.out.print(countPair(a, b));}}Â
// This code is contributed by PrinciRaj1992 |
Python3
# Python3 implementation of the approachÂ
# Function to return the number of# pairs satisfying the equationdef countPair(a, b):         # Converting integer b to string    # by using to_function    s = str(b)Â
    # Loop to check if all the digits    # of b are 9 or not    i = 0    while i < (len(s)):Â
        # If '9' doesn't appear        # then break the loop        if (s[i] != '9'):            break        i += 1Â
    result = 0Â
    # If all the digits of b contain 9    # then multiply a with length    # else multiply a with length - 1    if (i == len(s)):        result = a * len(s)    else:        result = a * (len(s) - 1)Â
    # Return the number of pairs    return resultÂ
# Driver codea = 5b = 101Â
print(countPair(a, b))Â
# This code is contributed by mohit kumar 29 |
C#
// C# implementation of the approachusing System;Â
class GFG{Â
// Function to return the number of// pairs satisfying the equationstatic int countPair(int a, int b){    // Converting integer b to String    // by using to_String function    String s = String.Join("", b);Â
    // Loop to check if all the digits    // of b are 9 or not    int i;    for (i = 0; i < s.Length; i++)    {Â
        // If '9' doesn't appear        // then break the loop        if (s[i] != '9')            break;    }Â
    int result;Â
    // If all the digits of b contain 9    // then multiply a with String length    // else multiply a with String length - 1    if (i == s.Length)        result = a * s.Length;    else        result = a * (s.Length - 1);Â
    // Return the number of pairs    return result;}Â
// Driver codepublic static void Main(String[] args){Â Â Â Â int a = 5, b = 101;Â
    Console.Write(countPair(a, b));}}Â
// This code is contributed by Rajput-Ji |
Javascript
<script>Â
// Javascript implementation of the approachÂ
// Function to return the number of// pairs satisfying the equationfunction countPair(a, b){         // Converting integer b to string    // by using to_string function    var s = (b.toString());Â
    // Loop to check if all the digits    // of b are 9 or not    var i;    for(i = 0; i < s.length; i++)    {                 // If '9' doesn't appear        // then break the loop        if (s[i] != '9')            break;    }Â
    var result;Â
    // If all the digits of b contain 9    // then multiply a with string length    // else multiply a with string length - 1    if (i == s.length)        result = a * s.length;    else        result = a * (s.length - 1);Â
    // Return the number of pairs    return result;}Â
// Driver codevar a = 5, b = 101;document.write(countPair(a, b));Â
// This code is contributed by rutvik_56Â
</script> |
10
Â
Time Complexity: O(b)
Auxiliary Space: O(1)
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!

… [Trackback]
[…] Read More on that Topic: geeksforgeeks.org/count-of-pairs-satisfying-the-given-condition/ […]
… [Trackback]
[…] Read More Information here to that Topic: geeksforgeeks.org/count-of-pairs-satisfying-the-given-condition/ […]
… [Trackback]
[…] Read More Info here to that Topic: geeksforgeeks.org/count-of-pairs-satisfying-the-given-condition/ […]