Q:

Flushing right an equation in flalign, moves it outside the page margin

I want to shift one equation to extreme right within align or flalign, but it moves outside the page margin. MWE is given below
\documentclass{article}
\usepackage{amsmath}
\usepackage{lipsum}
\begin{document}
    \lipsum[1] \setlength{\abovedisplayskip}{0pt} \setlength{\abovedisplayshortskip}{0pt}
    \begin{flalign*}
    x&=(a+b^2)(a+c^2+ sqrt(a+b^2))(a+b^2)(a+c^2+ sqrt(a+b^2))\\
    &=z(a+c^2 +sqrt(z)) z(a+c^2 +sqrt(z))\\
&&where \ \ z=(a+b^2)
%   \sin 2x &= 2\sin x\cos x \\ 
%   &&\cos 2x &= \cos^2 x-\sin^2 x
    \end{flalign*}
    \lipsum[2]
\end{document}

A:

I’d do this way:
\documentclass{article}
\usepackage{amsmath}
\usepackage{lipsum}
\begin{document}
    \lipsum[1] \setlength{\abovedisplayskip}{0pt} \setlength{\abovedisplayshortskip}{0pt}
    \begingroup
        \setlength{\multlinegap}{0pt}
        \begin{multline*}
            x=(a+b^2)(a+c^2+\sqrt(a+b^2))(a+b^2)(a+c^2+\sqrt(a+b^2))\\
            =z(a+c^2+\sqrt(z)) z(a+c^2+\sqrt(z))\\
            \text{where } z=(a+b^2)
        \end{multline*}
    \endgroup
    \lipsum[2]
    And another use of \texttt{multline*}:
    \begin{multline*}
        a=\\b=\\c
    \end{multline*}
    As you can see, \verb|\multilinegap| has again the default value.
\end{document}

Here’s the output:

Addition:  If you insist on getting the first two rows aligned at the = sign, the following is perhaps the most straightforward solution:
\documentclass{article}
\usepackage{amsmath}
\usepackage{lipsum}
\begin{document}
    \lipsum[1] \setlength{\abovedisplayskip}{0pt} \setlength{\abovedisplayshortskip}{0pt}
    \begingroup
        \setlength{\multlinegap}{0pt}
        \begin{multline*}
            \begin{aligned}
                x&=(a+b^2)(a+c^2+\sqrt(a+b^2))(a+b^2)(a+c^2+\sqrt(a+b^2))\\
                &=z(a+c^2+\sqrt(z)) z(a+c^2+\sqrt(z))
            \end{aligned} \\
            \text{where } z=(a+b^2)
        \end{multline*}
    \endgroup
    \lipsum[2]
    And another use of \texttt{multline*}:
    \begin{multline*}
        a=\\b=\\c
    \end{multline*}
    As you can see, \verb|\multilinegap| has again the default value.
\end{document}

Output:

Second addition: And don’t forget that there is also the magic \hidewidth:
\documentclass{article}
\usepackage{amsmath}
\usepackage{lipsum}
\begin{document}
    \lipsum[1] \setlength{\abovedisplayskip}{0pt} \setlength{\abovedisplayshortskip}{0pt}
    \begin{flalign*}
        x&=(a+b^2)(a+c^2+\sqrt(a+b^2))(a+b^2)(a+c^2+\sqrt(a+b^2))\\
        &=z(a+c^2+\sqrt(z)) z(a+c^2+\sqrt(z))\\
        &&\hidewidth \text{where } z=a+b^2
    \end{flalign*}
    \lipsum[2]
\end{document}

Output:

