Connection problems for polynomial solutions of nonhomogeneous differential and difference equations