Professional Documents
Culture Documents
#include <bits/stdc++.h>
Reg - 20BCE0191
Given a subset we need to check if the given sum can be formed from the values of sunset.
We use dynamic programming approach to solve this. Using tabulation method and bottom-up
approach
to fill the table checking the sum in each step and marking 1 or 0 if sum is possible*/
int i, j;
// Using the tabulation method to store the sum of subsets and check with given input sum.
// The rows have elements of the subset and columns have numbers ranging from 1 to sum+1.
dp[i][0] = 1;
// Initializing the column row value as 0
dp[0][j] = 0;
// Using two for loop for iterating the table, first for loop to iterate the rows and
// Here, we are checking if the index of column (the numbers ranging from 1 to sum value)
if (j < a[i])
// If the subset value is less column value, then inserting the previous row value (0 or 1)
// If not then performing the logical OR of previous row value with the current element index
value.
// [j-a[i]] where j is the column index and a[i] is the value present in the subset.
else
// Our reuslt is stored in the last cell of the table. If the last cell value is 1 then
// Checking in the table if the given sum found then displaying appropriate msg
if (dp[n][sum] == 1)
// If the last cell of the table is 1 then sum can be obtained from values of subset
else
int main()
int n, sum;
cin >> n;
int a[n];
auto start=chrono::steady_clock::now();
subset_sum(a, n, sum);
auto end=chrono::steady_clock::now();
auto diff=end-start;
cout<<chrono::duration<double,milli>(diff).count()<<"ms"<<endl;
}
#include <bits/stdc++.h>
/*Approach - In Karatsuba algorithm we split the numbers to multiply ito digits for
int count = 0;
while (num > 0) // Iterating the number and consecutive division to find its length
// Dividing the number by 10 and then increment the count to calcualte number of digits
count++;
num /= 10;
return count;
// Base Case
// Spliting X and Y
int b = X % p;
int d = Y % p;
int ac = karatsuba(a, c); /*Recursively spliting the number into futher multiplication*/
int bd = karatsuba(b, d); /*Recursively spliting the number into futher multiplication*/
int main(){
int b1,b2;
cin>>b1;
cin>>b2;
auto start=chrono::steady_clock::now();
int ans=karatsuba(b1,b2);
auto end=chrono::steady_clock::now();
auto diff=end-start;
cout<<chrono::duration<double,milli>(diff).count()<<"ms"<<endl;
#include<bits\stdc++.h>
#define N 4
The rat can move only in two directions: forward and down.
In the maze matrix, 0 means the block is a dead end and 1 means the block can be used
cout<<sol[i][j]<<" ";
cout<<endl;
/* Function to check if don't go outside the maze i.e., we don’t exit the maze */
if(x >= 0 && x < N && y >= 0 && y < N && maze[x][y] == 1)
return true;
// If the above constraints fail then we come outside the maze and hence no solution
return false;
path is possible, otherwise return true and prints the path in the
// Declaring 2d array to store the solution and intitially all vertices are 0
{0, 0, 0, 0},
{0, 0, 0, 0},
{0, 0, 0, 0}
};
return false;
printSolution(sol);
return true;
// Bae condition for recursive if we reach the last row and column of the maze then return
sol[x][y] = 1;
return true;
if(isSafe(maze, x, y) == true)
sol[x][y] = 1;
return true;
/* If none of the above movements then we backtrack and again try a different
sol[x][y] = 0;
return false;
return false;
int main()
int maze[N][N];
for(int i=0;i<N;i++){
for(int j=0;j<N;j++){
cin>>maze[i][j];
cout<<"Solutions : "<<endl;
auto start=chrono::steady_clock::now();
solveMaze(maze);
auto end=chrono::steady_clock::now();
auto diff=end-start;
cout<<chrono::duration<double,milli>(diff).count()<<"ms"<<endl;
return 0;
#include <bits\stdc++.h>
#define V 5
and the Hamiltonian cycle or circuit is a Hamiltonian path, that there is an edge from the last vertex
to the first vertex.*/
// Hamiltonian cycle. Here the path array has the vertices present in cycle
bool isSafe(int v, bool graph[V][V], int path[], int pos)
added vertex. */
if (graph [path[pos-1]][v] == 0)
return false;
if (path[i] == v)
return false;
return true;
if (pos == V)
// first vertex
return true;
else
return false;
{
// After processing the vertex checking if we can include it in cycle
path[pos] = v;
return true;
path[pos] = -1;
return false;
// Basically we process the vertex then then assuming it as main vertex checking
// Dynamically creating the path array as we before don’t know how many vertices can be there in
cycle
path[i] = -1;
// The first node of the graph would be added in the array(Hamiltonian cycle)
path[0] = 0;
// If the vertex doesn’t create continuous circuit with other vertices, then
return false;
printSolution(path);
return true;
cout<<path[i]<<" ";
cout<< path[0]<<endl;
int main()
bool graph[V][V];
for(int i=0;i<V;i++){
for(int j=0;j<V;j++){
cin>>graph[i][j];
}
}
auto start=chrono::steady_clock::now();
hamCycle(graph);
auto end=chrono::steady_clock::now();
auto diff=end-start;
cout<<chrono::duration<double,milli>(diff).count()<<"ms"<<endl;
return 0;