Beautiful numbers

3.7

13 votes
Algorithms, Basics of Greedy Algorithms, Greedy Algorithms
Problem

Any number is called beautiful if it consists of 2N digits and the sum of the first N digits is equal to the sum of the last N digits. Your task is to find the count of beautiful numbers in the interval from L to R (including L and R).

Beautiful numbers do not have leading zeroes.

Input format

  • The first line contains an integer T denoting the number of test cases.
  • The first line of each test case contains two space-separated integers L and R denoting the range interval [L,R].

Output format

For each test case, print the count of beautiful numbers in a new line.

Constraints

1T2000001LR1000000000

Sample Input
1
1 100
Sample Output
9
Time Limit: 2
Memory Limit: 256
Source Limit:
Explanation

There are only 9 beautiful numbers in the first 100 integers. 11,22,33,44,55,66,77,88 and 99 are the beautiful numbers in the range [1,100].

Editor Image

?