Before trying to find a UMP test, one needs to first check if there exists one. To do this one needs to find the likelihood ratio function
$$l(x)=f_{\theta_1}(x)/f_{\theta_0}(x)$$
This function must be monotone non-decreasing in $x$ for every $\theta_1\geq \theta_0$. In the given question $\theta_1=2$, and the density function is $$f_{2}(x)=2x.$$ Similarly for $\theta_0\in[1/2,1]$, $$f_{\theta_0}(x)=\theta_0x^{\theta_0-1}$$ Hence, the likelihood ratio function is
$$l_{\theta_0}(x)=\frac{2x}{\theta_0x^{\theta_0-1}}=\frac{2}{\theta_0}x^{2-\theta_0}$$
Since this function is increasing in $x$ for all $\theta_0\in[1/2,1]$, there exists a UMP test of level $\alpha$.
By definition of UMP test, the significance level $\alpha$ is the expected value of the decision rule (which is the likelihood ratio test with a certain threshold $\lambda$), for which the false alarm probability lies below $\alpha$, for every $\theta_0$
$$\alpha=\sup_{\theta_0}\int_{\{x:l_{\theta_0}(x)>\lambda\}}f_{\theta_0}(x)\mathrm{d}x=\sup_{\theta_0}\int_{\{x:l_{\theta_0}(x)>\lambda\}}\theta_0x^{\theta_0-1}\mathrm{d}x$$
Now, we have a nice simplification (Why?) $${\{x:l_{\theta_0}(x)>\lambda\}}\equiv {\{x:x>\lambda^{'}\}}$$
Hence
$$\alpha=\sup_{\theta_0}\int_{\{x:l_{\theta_0}(x)>\lambda\}}\theta_0x^{\theta_0-1}\mathrm{d}x=\sup_{\theta_0}\int_{\lambda^{'}}^1\theta_0x^{\theta_0-1}\mathrm{d}x=\sup_{\theta_0}1-{\lambda^{'}}^{\theta_0}=0.05$$
It is known that $\lambda^{'}\in[0,1]$ and $\theta_0\in[1/2,1]$. Now what value of $\theta_0$ maximizes $1-{\lambda^{'}}^{\theta_0}$ or similarly minimizes ${\lambda^{'}}^{\theta_0}$?
The UMP test is then $$\phi(x)=\begin{cases}1,\quad x>\lambda^{'}\\0,\quad x\leq \lambda^{'}\end{cases}$$
We have the distribution of a single observation $X$ :
\begin{align}
f_{\theta}(x)&=\frac{1}{\theta}\mathbf1_{x\in\{1,2,\ldots,\theta\}}\quad,\,\theta\in\{20,40\}
\end{align}
By NP lemma, an MP test of level $\alpha$ for testing $H_0:\theta=40$ against $H_1:\theta=20$ is of the form
\begin{align}
\varphi(x)&=\begin{cases}1&,\text{ if }\lambda(x)>k\\\gamma&,\text{ if }\lambda(x)=k\\0&,\text{ if }\lambda(x)<k\end{cases}
\end{align}
, where $$\lambda(x)=\frac{f_{H_1}(x)}{f_{H_0}(x)}$$
and $\gamma\in[0,1]$ and $k(> 0)$ are so chosen that $$E_{H_0}\,\varphi(X)\leqslant 0.1$$
Now,
\begin{align}
\lambda(x)&=2\frac{\mathbf1_{x\in\{1,2,\ldots,20\}}}{\mathbf1_{x\in\{1,2,\ldots,40\}}}
\\\\&=\begin{cases}2&,\text{ if }x=1,2,\ldots,20
\\0&,\text{ if }x=21,22,\ldots,40
\end{cases}
\end{align}
Therefore, for some $c$, $$\lambda(x)\gtrless k\implies x\lessgtr c$$
And the level restriction gives $$P_{H_0}(X<c)+\gamma P_{H_0}(X=c)\leqslant 0.1\tag{1}$$
Taking different values of $c$ (namely $c=2,3,4,5$) and finding the corresponding tail probability $P_{H_0}(X<c)$ subject to $(1)$, I end up with $$c=4\quad,\quad \gamma=1$$
So the required test is $$\varphi(x)=\mathbf1_{x\leqslant 4}$$
This is UMP because $\varphi$ obviously does not depend on the value of $\theta$ under $H_1$.
Best Answer
We have $\lambda\leq X\leq \lambda +1$ and $\mu\leq Y\leq \mu+1$ a.s. Define $Z=Y-X$. Then $\mu-\lambda-1\leq Z\leq \mu+1-\lambda$ so that $Z \in [\gamma-1,\gamma+1]$ a.s. where $\gamma=\mu-\lambda$. So $H_0:\gamma \leq 0$ and $H_1:\gamma >0$. The Lebesgue density of $Z$ is $f_\gamma(x)=(1-|x-\gamma|)\mathbf{1}_{[0,1]}(|x-\gamma|)$. Now let $\gamma'>\gamma$ be arbitrary. We have $f_{\gamma'}(x)/f_\gamma(x)$ nondecreasing in $x$ whenever at least one of the densities is positive, so it has monotone likelihood in $x$. Then there exists a UMP test of size $\alpha$ of type $$T(x)=\begin{cases} 1&x>c\\ u&x=c\\ 0&x<c \end{cases}$$ We have $P_\gamma(Z=u)=0$ for any $u,\gamma$ so we set $u=0$. We get $E_{\gamma=0}[T(Z)]=P_{\gamma=0}(Z>c)=\alpha$. If $0<\alpha<1/2$ we have $0<c<1$ so $$\int_{c}^1(1-x)dx=\alpha\implies (1-c)-\frac{1-c^2}{2}=\alpha\implies c=1-\sqrt{2\alpha}$$ So if $Z>1-\sqrt{2\alpha}$ we reject $H_0:\gamma\leq 0$.