CodeFights - Comfortable Numbers - Solution

Problem

Let's say that number a feels comfortable with number b if a ≠ b and b lies in the segment [a - s(a), a + s(a)], where s(x) is the sum of x's digits.
How many pairs (a, b) are there, such that a < b, both a and b lie on the segment [L, R], and each number feels comfortable with the other?
Example
For L = 10 and R = 12, the output should be
comfortableNumbers(L, R) = 2.
Here are all values of s(x) to consider:
  • s(10) = 1, so 10 is comfortable with 9 and 11;
  • s(11) = 2, so 11 is comfortable with 91012 and 13;
  • s(12) = 3, so 12 is comfortable with 910111314 and 15.
Thus, there are 2 pairs of numbers comfortable with each other within the segment [10; 12](10, 11) and (11, 12).
Input/Output
  • [time limit] 500ms (cpp)
  • [input] integer L
    Guaranteed constraints:
    1 ≤ L ≤ R ≤ 1000.
  • [input] integer R
    Guaranteed constraints:
    1 ≤ L ≤ R ≤ 1000.
  • [output] integer
    The number of pairs satisfying all the above conditions.

Solution

The problem is pretty straightforward due to constraints of the problem. We need to look at 1000C2 pairs in the worst case. We look at all the pairs in the range [L, R] and check if the pair of integers in a given pair are comfortable to each other.

Code

int digitSum(int n){
    int _sum = 0;
    
    while (n){
        _sum += (n%10);
        n = n/10;
    }
    
    return _sum;
}

int comfortableNumbers(int L, int R) {
    int total_pairs = 0;
    
    for(int i=L; i<=R; i++){
        for(int j=i+1; j<=R; j++){
            int s_a = digitSum(i);
            int s_b = digitSum(j);
            if (j>= (i-s_a) and j<= (i+s_a) and 
                  i>= (j-s_b) and i<= (j+s_b)
               ) {
                total_pairs++;
            }
        }
    }
    return total_pairs;
}

15 comments:

  1. there are many iphone app development company but only few are good and this blog need to convert into a beautiful website.

    ReplyDelete
  2. You know your projects stand out of the herd. There is something special about them. It seems to me all of them are really brilliant! seo expert

    ReplyDelete
  3. Maybe you have schoolwork, family photos, a book you wrote, or banking information you do not want anyone to touch. Getintopc

    ReplyDelete
  4. A portion of the main load-testing instruments utilized by engineers all inclusive are IBM Rational Performance Tester, Apache JMeter, LoadRunner and so on. microsoft registry cleaner

    ReplyDelete
  5. I was very pleased to find this site.I wanted to thank you for this great read!! I definitely enjoying every little bit of it and I have you bookmarked to check out new stuff you post  http://www.getinntopc.com

    ReplyDelete
  6. However as this OS was simply and extension of https://www.alcodasoftware.com any earlier OS it became more and more difficult to made enhancements to it without compromising on the speed of the system,

    ReplyDelete
  7. Software testers can perform execution of these test cases to highlight any issues to the development team as early as possible. Filehippo

    ReplyDelete
  8. A way to solve the problem in a more concise manner is as follows.

    def s(a):
    return sum([int(x) for x in str(a)])
    def comf(a, b):
    return a != b and b in range(a-s(a), a+s(a)+1)
    def comfortableNumbers(l, r):
    return len([(a, b) for a in range(l, r+1) for b in range(a+1, r+1) if comf(a, b) and comf(b, a)])

    The execution times should be similar

    ReplyDelete
  9. Maybe you have schoolwork, family photos, a book you wrote, or banking information you do not want anyone to touch.
    igg games

    ReplyDelete
  10. It was nice unblock game . Very informative and expressive .things are easily play online at
    unblock games

    ReplyDelete
  11. I am very Thankful to The Admin and Please Buddy Share your Knowledge Time to Time .
    filehippo
    file hippo
    File hippo
    Download Free PC Software
    File hippo

    ReplyDelete
  12. A part of the principle load-testing instruments used Toward particular architects all comprehensive are IBM normal execution Tester, apache JMeter, LoadRunner et cetera.

    ReplyDelete
  13. good information presented by the admin..thank you

    ReplyDelete
  14. At long last, you should institutionalize your code to the level that every designer will comprehend the goals behind the program's advancement. www.adlist24.com

    ReplyDelete
  15. Through this information, all your activities can be tracked and traced, invading your privacy. Windows washer is a tool that help remove all these traces, and much more to help run your computer better and faster.Windows US

    ReplyDelete