content: import java.util.ArrayList;
  import java.util.List;
  
  public class NQueens {
  
      public static List<List<String>> getNQueensArrangements(int queens) {
          List<List<String>> arrangements = new ArrayList<>();
          getSolution(queens, arrangements, new int[queens], 0);
          return arrangements;
      }
  
      public static void placeQueens(final int queens) {
          List<List<String>> arrangements = new ArrayList<>();
          getSolution(queens, arrangements, new int[queens], 0);
          if (arrangements.isEmpty()) {
              System.out.println("There is no way to place " + queens + " queens on board of size " + queens + "x" + queens);
          } else {
              System.out.println("Arrangement for placing " + queens + " queens");
          }
          for (List<String> arrangement : arrangements) {
              arrangement.forEach(System.out::println);
              System.out.println();
          }
      }
  
      private static void getSolution(int boardSize, List<List<String>> solutions, int[] columns, int columnIndex) {
          if (columnIndex == boardSize) {
              List<String> sol = new ArrayList<>();
              for (int i = 0; i < boardSize; i++) {
                  StringBuilder sb = new StringBuilder();
                  for (int j = 0; j < boardSize; j++) {
                      sb.append(j == columns[i] ? "Q" : ".");
                  }
                  sol.add(sb.toString());
              }
              solutions.add(sol);
              return;
          }
  
          for (int rowIndex = 0; rowIndex < boardSize; rowIndex++) {
              columns[columnIndex] = rowIndex;
              if (isPlacedCorrectly(columns, rowIndex, columnIndex)) {
                  getSolution(boardSize, solutions, columns, columnIndex + 1);
              }
          }
      }
  
      private static boolean isPlacedCorrectly(int[] columns, int rowIndex, int columnIndex) {
          for (int i = 0; i < columnIndex; i++) {
              int diff = Math.abs(columns[i] - rowIndex);
              if (diff == 0 || columnIndex - i == diff) {
                  return false;
              }
          }
          return true;
      }
  
      public static void main(String[] args) {
          placeQueens(4); // You can change N here
      }
  }
  
  