# In how many $4$-digit numbers the sum of two right digits is equal to the sum of two left digits

In how many $4$-digit numbers the sum of two right digits is equal to the sum of two left digits.

My attempt:We should find number of two pairs that can be digits of this number for choosing the place of digits we have $*8$ (We should notice we cant have $0$ in the beginning.But the biggest problem is finding such pairs.How can I find them?

• separate according to the sum of digits and you get a sum of $18$ terms, each term is a product of two integers. – Jorge Fernández Hidalgo Dec 29 '16 at 7:50
• @JorgeFernándezHidalgo Why we can get $18$ terms?Ant by integers you mean natural numbers? – Taha Akbari Dec 29 '16 at 7:54

First let's not bother with numbers starting with $0$ for now, we can subtract those cases later. Now consider what possible value can the sum $s$ of two digits be? We see that $s \in \{0,1,\dots,18\}$. Let's look at possible ways to get individual values of $s$:

\begin{array}{ccc} s& &\#\\ 0 & 00& 1\\ 1 & 10,01&2\\ 2 & 20,11,02&3\\ &\vdots\\ 9 & 90,81,72,63,54,45,36,27,18,09&10\\ 10 & 91,82,73,64,55,46,37,28,19&9\\ 11 & 92,83,\dots,29&8\\ &\vdots\\ 18 & 99&1\\ \end{array} Now if we want to get number of $4$-digit numbers where both pair of numbers give sum $s=0$, we have $1\cdot 1$ possibilities ($0000$), for sum $s=1$ we have $2\cdot 2=4$ possibilities ($1010$,$1001$,$0110$,$0101$), etc. So overall we have $$1\cdot1+2\cdot2+\dots+10\cdot10+9\cdot9+\dots+1\cdot 1=670.$$ Now let's subtract those combinations that begin with $0$. By inspecting the table above you can notice there is exactly one such pair that begins with $0$ for each $s\leq 9$. So this gives $1$ possibility for first pair of numbers, and original number of possibilities for second pair of numbers. So we have $$1\cdot1+1\cdot2+\dots+1\cdot10=55.$$ So there are $55$ $4$-digit numbers that begin with $0$ and satisfy your condition. Now just subtract those to values to get final result: $$670-55=615.$$

You can simplify the notation a bit by putting this into fancy sums and so, but I think in this case it is unnecessary (perhaps with larger values).

Take the number as $a_0a_1a_2a_3$.

For a sum of two digits $S = a_0 + a_1$, we can see that there are $19-S$ pairs of digits associated with the sum when $S \geq 10$ and $S +1$ pairs of digits otherwise. But, we require that $a_0\neq 0$. So, for the latter case, there are only $S$ digits as for each $S$ there is one combination with zero leading digit.

We thus take the sum as: $$\sum_{n=1}^{9} n(n+1) +\sum_{n =10}^{18} (19-n)^2 = 615$$ Hope it helps.

P.S: A Turbo C++ code also gives us the result as $615$.

• Could you please explain why it is $19-S$ when $S \ge 10$?And why $n+1$ otherwise? – Taha Akbari Dec 29 '16 at 8:04
• @TahaAkbari If the sum S =11, then we can have (2,9);(3,8);(4,7);(5,6);(6,5);(7,4);(8,3);(9,2). If the sum S=12, then we can have (3,9);(4,8);(5,7);(6,6);(7,5);(8,4);(9,3). Generalising, we get the result. – Rohan Dec 29 '16 at 8:08
• You can similarly try writing such pairs for S<10. – Rohan Dec 29 '16 at 8:09

This problem can be solved using Stars and Bars method.

Let the digits be $a,b,c,d$.

Then we must find integral solutions of $$a+b=c+d \Rightarrow a+b-c-d=0$$

with restrictions on $a,b,c,d\$ i.e $1\le a\le 9$ and $0\le \{b,c,d\}\le 9$

The equation can be transformed by taking $a=x_1+1,\ b=x_2,\ c=9-x_3,\ d=9-x_4$ to:

\begin{align}&x_1+1+x_2+x_3-9+x_4-9=0\\\Rightarrow \ \ &x_1+x_2+x_3+x_4=17\end{align}

with $0\le x_1\le 8$, $0\le \{x_2,x_3,x_4\}\le 9$

The number of integral solutions of above equation can be solved using the Stars and Bars method.

Try yourself or see below for rest of the procedure:

Number of solutions for $$x_1+x_2+x_3+ x_4 = 17\\$$ where $x_i \ge 0$ for $i = 1,2,3,4$ is $\binom{17+4-1}{17}=\binom{20}{3}$ solutions.

We have to remove cases where $x_1\ge9$ and $\{x_2,x_3,x_4\}\ge10$, let's say $x_1$ for example, is $\ge 9$, then we can find all the number of solutions to be excluded by putting $y_1 = x_1 - 9$, we can write $$y_1 + x_2 +x_3+x_4 = 8$$ which has $\binom{8+4-1}{8}=\binom{11}{3}$ solutions.

Similarly for $x_i\ge 10$ for $i=2,3,4$, then we can find all the number of solutions to be excluded by putting $y_i = x_i - 10$, we can write, for e.g. $x=2$, $$x_1 + y_2 +x_3+x_4 = 7$$ which has $\binom{7+4-1}{8}=\binom{10}{3}$ solutions.

Hence total number of such 4-digit numbers is: $$\binom{20}{3}-\binom{11}{3}-3\times\binom{10}{3}=615$$

The two digit sum $T$ can be a number between $0$ and $18$ (we can worry about zeroes presently). For values of $T$ up to $9$, we can $T$ between digits in $T{+}1$ ways, and for values from $9$ to $18$ we can split them in $18-T+1=19-T$ ways. This applies independently to the first two digits and the last, except that for $T$ values up to $9$, there are only $T$ ways to split the front digit pair due to the need to keep a zero off the front.

So the total looks like $$\sum_{T=0}^9 T(T+1) + \sum_{T=10}^{18} (19-T)^2 = \sum_{T=1}^9 \big((T^2 + T) + T^2\big) \\ = 2\sum_{T=1}^9 T^2 + \sum_{T=1}^9T = 2\cdot \frac{9\cdot 10\cdot 19}{6} + \frac{9\cdot 10}{2} \\ = 570 + 45 = 615$$

You have to check pattern. Many times we have patterns in question.

In this question first two digits makes sum equal to last two digits. And its clear that for first two digits we have no case in which first digit is zero.

Now we have to find number of ways.

Sum of first two and last two digits from 1 to 9 -

Case 1:

Sum = 1

We have (1,0) for left and (1,0),(0,1) for right.

Number of ways = 1*2

Case 2:

Sum = 2

We have (1,1)(2,0) for left and (1,1),(2,0),(0,2) for right.

Number of ways = 2*3

Case 3:

Sum = 3

We have (1,2),(2,1),(3,0) for left and (1,2),(2,1),(3,0),(0,3) for right.

Number of ways = 3*4

As you can see if sum is 1 then 1 way for left digits and (left + 1) ways for right. If sum 2 then 2 ways for left and 3 for right. Right = left + 1 ways.

Case 9:

So for sum = 9. We have 9 ways for left and 10 for right digits.

Number of ways = 9*10

Combining this. We have 1*2+2*3+3*4+4*5+......+9×10

= $\frac{n(n+1)(n+2)}{3}$

Here n is number of terms.

= $\frac{9×10×11}{3}$

= 330

Sum of first two and last two digits from 10 to 18 -

Case 1:

Sum = 10

We have (1,9),(2,8),(3,7),(4,6),(5,5),(6,4),(7,3),(8,2),(9,1) for left and (1,9),(2,8),(3,7),(4,6),(5,5),(6,4),(7,3),(8,2),(9,1) for right.

Number of ways = 9*9

Case 2:

Sum = 11

We have (2,9),(3,8),(4,7),(5,6),(6,5),(7,4),(8,3),(9,2) for left and (2,9),(3,8),(4,7),(5,6),(6,5),(7,4),(8,3),(9,2) for right.

Number of ways = 8*8

Case 3:

Sum = 12

We have (3,9),(4,8),(5,7),(6,6),(7,5),(8,4),(9,3) for left and (3,9),(4,8),(5,7),(6,6),(7,5),(8,4),(9,3) for right.

Number of ways = 6*6

**Case 9:*"

So on for sum = 18

We have (9,9) for left digits and (9,9) for right digits.

Number of ways = 1*1

From sum 10 to 18 we have left and right ways equal.

Combining this we have-

9*9+8*8+7*7+........+1*1

= $\frac{n(n+1)(2n+1)}{6}$

Here n is number of terms.

= $\frac{9*10*19}{6}$

= 285

So total number of ways = 330 + 285 = 615

The best way to tackle this problem is to think about what the sum of the two rightmost digits is going to be, and then find in how many ways that can be done.

Because we are summing only two 1-digit integers, the biggest sum we can get is with $9+9=18$ so there is no 4-digit number, whose sum of the two right digits gives more than $18$. Likewise it will never be less than $0$ because digits are never negative.

Therefore one can reason separately for all the possible values of the sum. We can start by noticing that for the sum to be $0$, then we must have the number ending with $00$ and starting with $00$ but that is not a valid 4-digit number as it is just $0$.

Suppose that the sum of the two left digits is some number $S$, with $S < 10$. How can you write $S$ as a sum of two 1-digit numbers, $S = a + b$, if $a$ represents the first digit of your number and $b$ the second? Well, the only restrictions are $1 \leq a \leq 9, 0 \leq b \leq 9$. Also, as soon as you fix the value for $a$, the value for $b$ becomes determined: $b = S - a$ hence one can only think of the possible values for $a$. But given that $S < 10$, $a$ can take any value in $1, 2, \cdots, S$. So if the sum is $S, S < 10$, there are $S$ possible arrangements for the left side of the number. What about the right side? Well, using a similar argument, but because the rightmost digit can be $0$, you see there are $S+1$ arrangements for the right hand side. Thus we know that part of our result is $\sum_{S=1}^{9} S(S+1) = 330$.

Now we only need to think about the possible arrangements of the digits if the sum of the two leftmost digits is $10\leq S\leq18$. Because $S$ is too big, $a$ can't be too small, otherwise you can't make $a + b = S$. For example, if $S=15$, you can't have $a = 1$ because $b$ can never be $14$ as it is just a single digit. So what we want to think about is: if I have $S$, what is the minimum value for $a$ that makes it possible for $b$ to be just a single digit?

We have $S = a + b \iff S - b = a$. Because $b$ can't be more than $9$, $a$ can't be less than $S - 9$. So we have $S-9\leq a \leq9$. This means that $a$ has $9 - (S - 9) + 1 = 19 - S$ possible values. After setting $a$, $b$ is determined. So all it takes is to think about the arrangements for the right side. But a similar reasoning shows that this too gives $19 - S$ arrangements when $10 \leq S \leq 18$ so we add another $\sum_{k=10}^{18} (19 - S)^2 = 285$.

Adding everything together, we get $330 + 285 = 615$ which thankfully is in accordance with the other answers.