Number of possible values of $4x-z$ if $x+y+z=20$ Given that $x, y,z$ are non negative integers such that $x+y+z=20$. If $S$ is the set of all possible values of $4x-z$.Find $n(S)$.
My try:
By stars and bars number of non negative integer solutions of $x+y+z=20$ is $\binom{22}{2}=210$
Among these $210$ ordered triplets of $(x,y,z)$ we need to find number of possible values taken by $4x-z$.
Obviously the least value taken by $4x-z$ is $-20$ when $x=0,z=20$.
The next value taken by $4x-z$ is $-19$ when $x=0,z=19$ and so on.
So once we fix $x=0$ ,since $z$ varies from $[0,20]$ number of values taken by $4x-z$ is $21$.
But when we fix $x=1$, we get some overlaps. For example when $x=1,z=18$ we have $4x-z=-14$ and this value has already been counted in the previous case when $x=0,z=14$.
So any way to count total number of values taken by $4x-z$ excluding overlaps?
 A: For a given value of $x$, $z$ can range from $0$ up to $20-x$. This means that
$$D_a := \{4a-z \mid a + y + z = 20\} = \{5a-20, 5a-19, \ldots, 4a\}$$
(note that $5a-20 \leq 4a$ when $0 \leq x \leq 20$) where $a$ is fixed. Notice also that $4a \geq 5(a+1)-20$ for $a \leq 15$. This means that $D_a$ and $D_{a+1}$ overlap for $a \leq 15$. As a result, we find that
$$D_0 \cup D_1 \cup D_2 \cup \cdots \cup D_{16} = \{5 \cdot 0 - 20, \ldots, 4 \cdot 16\} = \{-20, \ldots, 64\}$$
For $a \geq 17$, $D_a$ is disjoint from $D_{a-1}$ and $D_{a+1}$. This means that
\begin{align}
n(\{4x-z \mid x + y + z = 20\}) &= n(D_0 \cup D_1 \cup \cdots \cup D_{20}) \\
&= n(D_0 \cup \cdots \cup D_{16}) + n(D_{17}) + n(D_{18}) + n(D_{19}) + n(D_{20}) \\
&= 85 + 4 + 3 + 2 + 1 = \boxed{95}
\end{align}
A: We can count the total possible unique values of $4x-z$ by summing the size of the following 5 mutually disjoint sets of values:

*

*Non-positive values: corresponds to $x=0$, there are $21$ such values of $4x-z$ from $-20$ to $0$, corresponding to $z=0,1,2,\ldots , 20$.


*Positive values of form $\equiv 0 \pmod 4$: corresponds to $z=0$ and $x=1,2,\ldots, 20$, there are 20 such values.


*Positive values of form $\equiv 3 \pmod 4$: corresponds to $z=1$ and $x=1,2,\ldots, 19$, there are 19 such values.


*Positive values of form $\equiv 2 \pmod 4$: corresponds to $z=2$ and $x=1,2,\ldots, 18$, there are 18 such values.


*Positive values of form $\equiv 1 \pmod 4$: corresponds to $z=3$ and $x=1,2,\ldots, 17$, there are 17 such values.
Generalizing the above mod 4 cases, we have the following mutually disjoint sets of values:

*

*Non-positive values: corresponds to $x=0$, there are $21$ such values of $4x-z$ from $-20$ to $0$, corresponding to $z=0,1,2,\ldots, 20$.


*Positive values of form $\equiv -k \pmod 4$: corresponds to $z=k$ and $x=1,2,\ldots, (20-k)$, there are $20-k$ such values of $4x-z$, for $k \in \{0,1,2,3\}$.
Hence, the total number of distinct values of $4x-z$ will be $=21+\sum\limits_{k=0}^{3}(20-k)=21+80-6=95$
A: Well the very least $x$ can be is $0$ and if so, you can have $z$ be any value from $0$ to $20$ inclusive (by letting $y = 20-z$). SO all (integer) values from $-20$ to $0$ are possible but no smaller are possible.
Now we just have to find all possible positive values.
Every positive integer is a multiple of $4$ minus either $0, 1,2$ or $3$ so if we set $z= 0,1,2$ or $3$ we can get set $x$ to any value from $1$ to $17$ (by letting $y = 20 - x-z$) so get all values up to $4\cdot 17 - 0= 68$.  So all values between $-20$ to $68$ inclusive are possible.
Now we just have to find all possible higher values.
And we can do those one by one.  If $n = 4x -z > 68$ and $z \ge 0$ then $x > 17$ so as $0\le x \le 20$ we can have $x = 18,19, 20$.
If $x = 18$ then $z = 20 -18 -y = 2-z$ so $z$ can be $0,1$ or $2$.  So that allows $6\cdot 18 - 0,1,2$ or $72,71,70$ but not $69$.
If $x = 19$ then $z =1-y$ and $z$ can be $0$ or $1$ so that allows $76,75$ (but not $74$ or $73$.
And $x = 20$ then $z=0$ and we have $80$ is possible but $79,78,77$ are not.
So the total number is $-20,....1$ and $0$ and $1.... 68$ and then $3$ and $2$ and $1$ more.  For a total of $20 + 1 + 68 + 3+ 2 + 1= 95$ values.
=====
Or more systematic.
$x$ con range from $0$ to $20$ and $z$ from $0$ to $20$ so $4x-z$ can run from $4\cdot 0 -20=-20$ to $4\cdot 20 - 0=80$
The times $n = 4a - b$ are not possible is if $a + b>20$
As $4a - b= 4(a+1)- (b+4)$ so if we have $n$ not possible so $a + b > 20$ we must also have either $(a+1)+(b-4)>20$ or $a+1 > 20$ or $b-4 < 0$.  So wolog we can assume that if $n=4a-b$ is impossible that we are assuming that $0\le b < 4$. And therefore $a+ b >20$ and $a > 20-b$.
For $a=20$ there are $3$ impossible values values for $b$.  For $a = 19$ there are $2$ impossible values.  For $a=18$ there is $1$. ANd for $a \le 17$ there are none.
So all values between $-20...,80$ are possible except for $1+2+3 = 6$ impossible values.
So there are $101-6 = 95$ possible values.
A: As @SomeGuy said, you can post programs, too.
Here is mine (in C++):
#include <iostream>
#include <unordered_set>
using namespace std;
int main()
{
    const int n = 5000;
    std::unordered_set<int> vec = {};
    for (int x = 0; x <= n; x++)
    {
        for (int y = 0; y <= n - x; y++)
        {
            int value = 5 * x - n + y;
            vec.insert(value);
        }
    }
    std::cout << vec.size();
}

The output is 95. It is incredibly efficient because of unordered_set.
