Professional Documents
Culture Documents
AIM:
(i) Write a program to implement N-queen problem
Problem link: https://www.hackerearth.com/practice/basic-programming/recursion/recursion-
and-backtracking/practice-problems/algorithm/n-queensrecursion-tutorial/
(ii) Write a program to implement Recursive digit sum problem.
Problem link: https://www.hackerrank.com/challenges/recursive-digit-
sum/problem?h_l=interview&isFullScreen=true&playlist_slugs%5B%5D%5B%5D=intervie
w-preparation-kit&playlist_slugs%5B%5D%5B%5D=recursion-backtracking
OVERVIEW OF THE PRACTICAL:
(i) Given a chess board having N×N cells, you need to place N queens on the board in such a way that no
queen attacks any other queen.
Input:
The only line of input consists of a single integer denoting N.
Output:
If it is possible to place all the N queens in such a way that no queen attacks another queen, then print N lines
having N integers. The integer in ith line and jth column will denote the cell (i,j) of the board and should be 1 if a
queen is placed at (i,j) otherwise 0. If there are more than way of placing queens print any of them. If it is not
possible to place all N queens in the desired way, then print "Not possible" (without quotes).
Complete the function superDigit in the editor below. It must return the calculated super digit as an integer.
Returns
for checking these three things i have made a funciton issafe(); and for solving the main problem
recursively(using backtracking) i have made a solve function.
Super-digit(n) = n, n<10
= super-digit(sum-of-digit(n)), otherwise
Note that since initially n can be very large, use a string representation to represent it for the first time.
//You have tried for all position in the current row but couldn't place a queen
return false;
/*
* Complete the 'superDigit' function below.
*
* The function is expected to return an INTEGER.
* The function accepts following parameters:
* 1. STRING n
* 2. INTEGER k
*/
RESULT:
(i)
(ii)