Professional Documents
Culture Documents
AA Record - 231011 - 165258
AA Record - 231011 - 165258
AA Record - 231011 - 165258
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>
int N;
int board[101][101];
return (x >= 0 && x < N && y >= 0 && y < N && board[x][y] == -1);
if (visited == N * N) {
if (isSafe(nextX, nextY)) {
board[nextX][nextY] = visited;
return true;
} else {
board[nextX][nextY] = -1;
return false;
}
void print() {
int main() {
cin >> N;
board[i][j] = -1;
int startX = 0;
int startY = 0;
board[startX][startY] = 0;
print();
} else {
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>
int board[15][15];
return (x >= 0 && x < n && y >= 0 && y < n && board[x][y] == -1);
int count = 0;
if(visited == n * n){
return 1;
board[nextx][nexty] = visited;
board[nextx][nexty] = -1;
return count;
int main(){
int n; cin>>n;
int x,y; cin>>x>>y;
board[i][j] = -1;
board[x - 1][y - 1] = 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>
int graph[4][4];
int col[4];
int c;
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;
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
}
}
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>
if (graph[path[pos - 1]][v] == 0)
return false;
if (path[i] == v)
return false;
return true;
if (pos == V) {
if (graph[path[pos - 1]][path[0]] == 1)
return true;
else
return false;
path[pos] = v;
return true;
path[pos] = -1;
return false;
path[0] = 0;
return false;
printSolution(path);
return true;
int main() {
int V;
cin >> V;
}
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>
if (grid[row][i] == num) {
return false;
if (grid[i][col] == num) {
return false;
if (grid[i][j] == num) {
return false;
return true;
}
bool solveSudoku(int grid[9][9], int row, int col) {
return true;
if (col == 9) {
row++;
col = 0;
if (grid[row][col] != 0) {
grid[row][col] = num;
return true;
grid[row][col] = 0;
return false;
int main() {
int grid[9][9];
if (solveSudoku(grid, 0, 0)) {
} else {
return 0;
}
Sieve of Sundaram:
Given a number n, print all primes smaller than or equal to n.
CODE
#include <iostream>
#include <vector>
void sieve(int n)
int num=(n-1)/2;
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>
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>
vector<int> set;
vector<int> prime;
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){
display();
return;
return;
set.push_back(prime[index]);
printsum(sum,n,total+prime[index],index+1);
set.pop_back();
printsum(sum,n,total,index+1);
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);
}