© 2020 The Author(s).Measurements of the second Fourier harmonic coefficient (v2) of the azimuthal distributions of prompt and nonprompt D0 mesons produced in pp and pPb collisions are presented. Nonprompt D0 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, v2 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 (pT) range of 2-6 GeV. Compared at similar event multiplicities, the prompt D0 meson v2 values in pp and pPb collisions are similar in magnitude. The v2 values for open beauty hadrons are extracted for the first time via nonprompt D0 mesons in pPb collisions. For pT in the range of 2-5 GeV, the results suggest that v2 for nonprompt D0 mesons is smaller than that for prompt D0 mesons. These new measurements indicate a positive charm hadron v2 in pp collisions and suggest a mass dependence in v2 between charm and beauty hadrons in the pPb system. These results provide insights into the origin of heavy-flavor quark collectivity in small systems.