Hey guys I need help on reducing this code time complexity, may someone please update the code below!
#include <iostream> using namespace std; #define N 16 void print(int arr[N][N]) { for (int i = 0; i < N; i++){ for (int j = 0; j < N; j++){ cout << arr[i][j]<< " "; if (j < N - 1){ cout << " "; } } cout << endl; } } bool isSafe(int grid[N][N], int row, int col, int num){ for (int x = 0; x < N; x++){ if (grid[row][x] == num || grid[x][col] == num){ return false; } } int boxSize = 4; // Since it's a 16x16 Sudoku, each box size is 4x4 int startRow = row - row % boxSize; int startCol = col - col % boxSize; for (int i = 0; i < boxSize; i++){ for (int j = 0; j < boxSize; j++){ if (grid[i + startRow][j + startCol] == num){ return false; } } } return true; } bool solveSudoku(int grid[N][N], int row, int col){ if (row == N - 1 && col == N){ return true; } if (col == N){ row++; col = 0; } if (grid[row][col] > 0){ return solveSudoku(grid, row, col + 1); } for (int num = 1; num <= N; num++){ if (isSafe(grid, row, col, num)){ grid[row][col] = num; if (solveSudoku(grid, row, col + 1)){ return true; } grid[row][col] = 0; } } return false; } int main(){ int grid[N][N]; for (int i = 0; i < N; i++){ for (int j = 0; j < N; j++){ cin >> grid[i][j]; } } if (solveSudoku(grid, 0, 0)){ print(grid); } else{ cout << "No Solution" << endl; } return 0; }