                      N Queens Problem
The N-Queens problem is a classic algorithmic puzzle that challenges you to place N non-attacking queens on an N × N chessboard. In chess, a queen can move horizontally, vertically, and diagonally. Therefore, no two queens can share the same row, column, or diagonal . 
The problem is most efficiently solved using a backtracking algorithm. Backtracking works by attempting to place a queen in a specific column of the current row and then verifying if it is safe. If it is safe, the algorithm moves to the next row. If it leads to a dead end where an attacking queen cannot be placed, the algorithm undoes the last move and tries a different column .
To optimize the process, developers can track the occupied columns and diagonals using sets or arrays to ensure placements are checked in constant time, O(1), instead of scanning the board repeatedly : 
Columns: Each queen must be in a distinct column.
Positive Diagonals: Can be tracked using the sum of the row and column indices (row + col).
Negative Diagonals: Can be tracked using the difference between the row and column indices (row - col).
Code:-
import copy
def take_input():
    while True:
        try:
            size = int(input("what is the size of the chess board ? n ="))
            if size==1:
                print("Trivial solution,choose size of atleast 4")
            if size<=3:
                print("Enter a value such that size>=4")
                continue
            return size
        except ValueError:
            print("Invalid value enter.Enter again")
def get_board(size):
    board=[0]*size
    for ix in range(size):
        board[ix]=[0]*size
    return board
def print_sol(solutions,size):
    for sol in solutions:
        for row in sol:
            print(row)
        print()
def is_safe(board,row,col,size):
    for iy in range(col):
        if board[row][iy]==1:
            return False
    ix,iy =row,col
    while ix>=0 and iy>=0:
        if board[ix][iy]==1:
            return False
        ix-=1
        iy-=1
    jx,jy=row,col
    while jx<size and jy>=0:
        if board[jx][jy]==1:
            return False
        jx+=1
        jy-=1
    return True
def solve(board,col,size):
    if col>=size:
        return
    for i in range(size):
        if is_safe(board,i,col,size):
            board[i][col]=1
            if col== size-1:
                add_solution(board)
                board[i][col]=0
                return
            solve(board,col+1,size)
            board[i][col]=0
def add_solution(board):
    global solutions
    saved_board=copy.deepcopy(board)
    solutions.append(saved_board)
size=take_input()
board=get_board(size)
solutions=[]
solve(board,0,size)
print_sol(solutions,size)
print("Total solutions ={}".format(len(solutions)))
size=take_input()
Solutions:-