Given a mechanical system with degrees of freedom $q=(q_1,\cdots,q_n)$, whose dynamics is given by
$$\ddot{q}_i = f_i(q,\dot{q},t),\tag{1}$$
for $i=1,\cdots,n$, are there criteria on the $f_i$'s which ensure the existence of a Lagrangian? I mean, of course, that the system (1) would be the Euler-Lagrange equations for that Lagrangian. iirc for $n=1$, it has been known since the 19th century, that there is always a Lagrangian. But what is known for more than one degree of freedom?
Actually, it comes to think of it that the system (1) might not be the Euler-Lagrange equations in general. Instead, we could have functions $a_{ij}(q, \dot{q},t)$ such that
$$a_{ij}(\ddot{q}_j-f_j)=\frac{d}{dt}\left(\frac{\partial L}{\partial \dot{q}_i}\right)-\frac{\partial L}{\partial q_i},$$
with a summation on repeated indices.