N - Queen Problem Solution with a similar approach: (runtime 2 ms)

public int totalNQueens(int n) { int[] solutions = {0}; // number of solutions. stored in array to retain value across recursive calls totalNQueens(n, 0, new boolean[n], new boolean[n * 2], new boolean[n * 2], solutions); return solutions[0]; } private void totalNQueens(int n, int current, boolean[] columns, boolean[] diagonal1, boolean[] diagonal2, int[] count) { if (current == n) { count[0]++; return; } for (int i = 0; i < n; i++) { // columns keep track of the column and diagonal 1 and 2 the diagonals if (columns[i] || diagonal1[i + current] || diagonal2[i - current + n - 1]) { continue; } columns[i] = diagonal1[i + current] = diagonal2[i - current + n - 1] = true; totalNQueens(n, current + 1, columns, diagonal1, diagonal2, count); columns[i] = diagonal1[i + current] = diagonal2[i - current + n - 1] = false; } }Subsets