  Measurements of the second Fourier harmonic coefficient ($v_2$) of the
azimuthal distributions of prompt and nonprompt D$^0$ mesons produced in pp and
pPb collisions are presented. Nonprompt D$^0$ mesons come from beauty hadron
decays. The data samples are collected by the CMS experiment at nucleon-nucleon
center-of-mass energies of 13 and 8.16 TeV, respectively. In high multiplicity
pp collisions, $v_2$ signals for prompt charm hadrons are reported for the
first time, and are found to be comparable to those for light-flavor hadron
species over a transverse momentum ($p_\mathrm{T}$) range of 2-6 GeV. Compared
at similar event multiplicities, the prompt D$^0$ meson $v_2$ values in pp and
pPb collisions are similar in magnitude. The $v_2$ values for open beauty
hadrons are extracted for the first time via nonprompt D$^0$ mesons in pPb
collisions. For $p_\mathrm{T}$ in the range of 2-5 GeV, the results suggest
that $v_2$ for nonprompt D$^0$ mesons are smaller than those for prompt D$^0$
mesons. These new measurements indicate a positive charm hadron $v_2$ in pp
collisions and suggest a mass dependence in $v_2$ between charm and beauty
hadrons in the pPb system. These results provide insights into the origin of
heavy-flavor quark collectivity in small systems.
