This allows for a full exploitation of the O(N 2 ) scaling of the Coulomb contribution. For the K matrix, the presorting is instead carried out for subgroups of the bra and ket pairs where the first primitive indices correspond to the subscript of the K matrix elements, such that a thread block can scan over relevant pairs to form a specific K matrix element (see