AA Record - 231011 - 165258

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 21

The Knight's Tour Problem:

Given a N*N board with the Knight placed on the first block of an empty board. Moving
according to the rules of chess knight must visit each square exactly once. Print the
order of each cell in which they are visited.
Write a program for the same.

CODE
#include<bits/stdc++.h>

using namespace std;

int N;

int board[101][101];

int xMove[] = {2, 1, -1, -2, -2, -1, 1, 2};

int yMove[] = {1, 2, 2, 1, -1, -2, -2, -1};

bool isSafe(int x, int y) {

return (x >= 0 && x < N && y >= 0 && y < N && board[x][y] == -1);

bool solveKT(int x, int y, int visited) {

if (visited == N * N) {

return true; // All cells have been visited

for (int i = 0; i < 8; i++) {

int nextX = x + xMove[i];

int nextY = y + yMove[i];

if (isSafe(nextX, nextY)) {

board[nextX][nextY] = visited;

if (solveKT(nextX, nextY, visited + 1)) {

return true;

} else {

board[nextX][nextY] = -1;

return false;
}

void print() {

for (int i = 0; i < N; i++) {

for (int j = 0; j < N; j++) {

cout << board[i][j] << " ";

cout << endl;

int main() {

cin >> N;

for (int i = 0; i < N; i++) {

for (int j = 0; j < N; j++) {

board[i][j] = -1;

int startX = 0;

int startY = 0;

board[startX][startY] = 0;

if (solveKT(startX, startY, 1)) {

print();

} else {

cout << "Solution does not exist";

return 0;

}
Subset Sum Problem:
Write a program to count the number of a subset of elements that are selected from
a given set whose sum adds up to a given number K. We are considering the set
contains non-negative values. It is assumed that the input set is unique (no
duplicates are presented).

CODE

#include<bits/stdc++.h>

using namespace std;

int board[15][15];

int x_arr[] = {2, 1, -1, -2, -2, -1, 1, 2};

int y_arr[] = {1, 2, 2, 1, -1, -2, -2, -1};

bool issafe(int n, int x, int y){

return (x >= 0 && x < n && y >= 0 && y < n && board[x][y] == -1);

int solve(int n, int x, int y, int visited){

int count = 0;

if(visited == n * n){

return 1;

for(int i = 0; i < 8; i++){

int nextx = x + x_arr[i];

int nexty = y + y_arr[i];

if(issafe(n, nextx, nexty)){

board[nextx][nexty] = visited;

count = count + solve(n, nextx, nexty, visited + 1);

board[nextx][nexty] = -1;

return count;

int main(){

int n; cin>>n;
int x,y; cin>>x>>y;

for(int i = 0; i < n; i++){

for(int j = 0; j < n; j++){

board[i][j] = -1;

board[x - 1][y - 1] = 0;

int ans = solve(n, x - 1, y - 1, 1);

if(ans > 0){

cout<<ans;

else{

cout<<-1;

}
m Coloring Problem:
Given an undirected graph and a number m, determine if the graph can be colored
with at most m colors such that no two adjacent vertices of the graph are colored
with the same color. Here coloring of a graph means the assignment of colors to all
vertices.

CODE

#include <bits/stdc++.h>

using namespace std;

int graph[4][4];

int col[4];

int c;

bool issafe(int cl,int in)

for(int i=0;i<=in;i++)

if(c>=4&&col[i]==cl)

return false;

if(graph[i][in]==1&&col[i]==cl)

return false;

return true;

bool color(int index)

if(index==4)

return true;

}
for(int cl=1;cl<=c;cl++)

if(issafe(cl,index))

col[index] = cl;

if(color(index + 1))

return true;

col[index]=0;

return false;

void solve()

memset(col,0,sizeof(col));

col[0]=1;

if(color(1))

cout<<"Solution Exists:\n";

for(int i=0;i<4;i++)

cout<<col[i]<<" ";

else

cout<<"Solution does not exist";

}
}

int main()

for(int i=0;i<4;i++)

for(int j=0;j<4;j++)

cin>>graph[i][j];

cin>>c;

solve();

}
Hamiltonian Cycle:
Determine whether a given undirected graph contains the Hamiltonian Cycle or not.
Hamiltonian Cycle in an undirected graph is a path that visits each vertex exactly
once. A Hamiltonian cycle is a Hamiltonian Path such that there is an edge from the
last vertex to the first vertex of the Hamiltonian Path.

CODE

#include <iostream>

#include <vector>

using namespace std;

void printSolution(const vector<int>& path);

bool isSafe(int v, const vector<vector<int>>& graph, vector<int>& path, int pos) {

if (graph[path[pos - 1]][v] == 0)

return false;

for (int i = 0; i < pos; i++)

if (path[i] == v)

return false;

return true;

bool hamCycleUtil(const vector<vector<int>>& graph, vector<int>& path, int pos, int V) {

if (pos == V) {

if (graph[path[pos - 1]][path[0]] == 1)

return true;

else

return false;

for (int v = 1; v < V; v++) {

if (isSafe(v, graph, path, pos)) {

path[pos] = v;

if (hamCycleUtil(graph, path, pos + 1, V) == true)

return true;
path[pos] = -1;

return false;

bool hamCycle(vector<vector<int>>& graph, int V) {

vector<int> path(V, -1);

path[0] = 0;

if (hamCycleUtil(graph, path, 1, V) == false) {

cout << "Hamiltonian path does not exist" << endl;

return false;

printSolution(path);

return true;

void printSolution(const vector<int>& path) {

for (int i = 0; i < path.size(); i++)

cout << path[i] << ' ';

cout << path[0] << endl;

int main() {

int V;

cin >> V;

vector<vector<int>> graph(V, vector<int>(V));

for (int i = 0; i < V; i++) {

for (int j = 0; j < V; j++) {

cin >> graph[i][j];

}
hamCycle(graph, V);

return 0;

}
Sudoku:
Given a partially filled 9×9 2D array ‘grid[9][9]’, the goal is to assign digits (from 1 to 9)
to the empty cells so that every row, column, and subgrid of size 3×3 contains exactly
one instance of the digits from 1 to 9.

CODE

#include <iostream>

using namespace std;

bool isSafe(int grid[9][9], int num, int row, int col) {

for (int i = 0; i < 9; i++) {

if (grid[row][i] == num) {

return false;

for (int i = 0; i < 9; i++) {

if (grid[i][col] == num) {

return false;

int startRow = row - (row % 3);

int startCol = col - (col % 3);

for (int i = startRow; i < startRow + 3; i++) {

for (int j = startCol; j < startCol + 3; j++) {

if (grid[i][j] == num) {

return false;

return true;

}
bool solveSudoku(int grid[9][9], int row, int col) {

if (row == 8 && col == 8) {

return true;

if (col == 9) {

row++;

col = 0;

if (grid[row][col] != 0) {

return solveSudoku(grid, row, col + 1);

for (int num = 1; num <= 9; num++) {

if (isSafe(grid, num, row, col)) {

grid[row][col] = num;

if (solveSudoku(grid, row, col + 1)) {

return true;

grid[row][col] = 0;

return false;

int main() {

int grid[9][9];

for (int i = 0; i < 9; i++) {

for (int j = 0; j < 9; j++) {

cin >> grid[i][j];

if (solveSudoku(grid, 0, 0)) {

for (int i = 0; i < 9; i++) {


for (int j = 0; j < 9; j++) {

cout << grid[i][j] << " ";

cout << endl;

} else {

cout << "No Solution exists" << endl;

return 0;

}
Sieve of Sundaram:
Given a number n, print all primes smaller than or equal to n.

CODE

#include <iostream>

#include <vector>

using namespace std;

void sieve(int n)

int num=(n-1)/2;

vector<bool> arr(num + 1, false);

for(int i=1;i<=num;i++)

for(int j=i;(j+i+2*i*j)<=num;j++)

arr[j + i + 2 * i * j] = true;

cout<<"2 ";

for(int i=1;i<=num;i++)

if(!arr[i])

cout<<2*i+1<<" ";

int main()

int n;

cin>>n;

sieve(n);
}
Prime number:
Check whether the given number(N) is Prime or not.

CODE

#include <bits/stdc++.h>

using namespace std;

int main()

int n;

cin>>n;

int c=0;

for(int i=1;i<=n;i++)

if(n%i == 0)

c++;

if(c == 2)

cout<<"Prime";

else

cout<<"Not Prime";

}
Prime Sum after prime number P:
Given three numbers sum S, prime P, and N, find all N prime numbers after prime P
such that their sum is equal to S.

CODE

#include<iostream>

#include<vector>

#include<cmath>

using namespace std;

vector<int> set;

vector<int> prime;

bool isprime(int p){

int c=0;

for(int i=1;i<=p;i++){

if(p%i==0){

c++;

if(c==2){

return true;

else{

return false;

void display(){

int l=set.size();

for(int i=0;i<l;i++){

cout<<set[i]<<"\t";

cout<<endl;

}
void printsum(int sum,int n,int total,int index){

if(total==sum && set.size()==n){

display();

return;

if(total>sum || index>=prime.size() || set.size()>=n){

return;

set.push_back(prime[index]);

printsum(sum,n,total+prime[index],index+1);

set.pop_back();

printsum(sum,n,total,index+1);

void allprime(int sum,int n,int p){

int primecount=0;

for(int i=p+1;i<sum;i++){

if(isprime(i)){

prime.push_back(i);

if(prime.size()<n)

return;

printsum(sum,n,0,0);

int main(){

int a,b,c;

cin>>a>>b>>c;

cout<<b<<" Prime numbers greater than "<<c<<" with sum = "<<a<<" are :\n";

allprime(a,b,c);
}

You might also like