Cold atoms in an optical cavity have been widely used for quantum simulations of many-body physics, where the quantum control capability has been advancing rapidly in recent years. Here, we show the atom cavity system is universal for quantum optimization with arbitrary connectivity. We consider a single-mode cavity and develop a Raman coupling scheme by which the engineered quantum Hamiltonian for atoms directly encodes number partition problems. The programmability is introduced by placing the atoms at different positions in the cavity with optical tweezers. The number partition problem solution is encoded in the ground state of atomic qubits coupled through a photonic cavity mode, which can be reached by adiabatic quantum computing. We construct an explicit mapping for the 3-SAT and vertex cover problems to be efficiently encoded by the cavity system, which costs linear overhead in the number of atomic qubits. The atom cavity encoding is further extended to quadratic unconstrained binary optimization problems. The encoding protocol is optimal in the cost of atom number scaling with the number of binary degrees of freedom of the computation problem. Our theory implies the atom cavity system is a promising quantum optimization platform searching for practical quantum advantage.