A mini max theorem and applications to nonresonance problems for semilinear equations