Charge-dependent azimuthal correlations of same-and opposite-sign pairs with respect to the second-and third-order event planes have been measured in pPb collisions at root s(NN) = 8.16 TeV and PbPb collisions at 5.02 TeV with the CMS experiment at the LHC. The measurement is motivated by the search for the charge separation phenomenon predicted by the chiral magnetic effect (CME) in heavy ion collisions. Three-and two-particle azimuthal correlators are extracted as functions of the pseudorapidity difference, the transverse momentum (p(T)) difference, and the p(T) average of same-and opposite-charge pairs in various event multiplicity ranges. The data suggest that the charge-dependent three-particle correlators with respect to the second-and third-order event planes share a common origin, predominantly arising from charge-dependent two-particle azimuthal correlations coupled with an anisotropic flow. The CME is expected to lead to a v(2)-independent three-particle correlation when the magnetic field is fixed. Using an event shape engineering technique, upper limits on the v(2)-independent fraction of the three-particle correlator are estimated to be 13% for pPb and 7% for PbPb collisions at 95% confidence level. The results of this analysis, both the dominance of two-particle correlations as a source of the three-particle results and the similarities seen between PbPb and pPb, provide stringent constraints on the origin of charge-dependent three-particle azimuthal correlations and challenge their interpretation as arising from a chiral magnetic effect in heavy ion collisions.