0% found this document useful (0 votes)
23 views36 pages

Lab Da 1

The document discusses design and analysis of algorithms lab assessment. It includes code snippets for various algorithms problems like longest common subsequence, knapsack problem, N-queens problem, and more.

Uploaded by

swastik raj
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views36 pages

Lab Da 1

The document discusses design and analysis of algorithms lab assessment. It includes code snippets for various algorithms problems like longest common subsequence, knapsack problem, N-queens problem, and more.

Uploaded by

swastik raj
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 36

Design anD analysis of algorithms lab

assesment

Name:- Swastik raj


Register no. :- 22BCE0411
Branch:- Computer science and engineering core
Faculty name:- Dr. Jenicka S
Slot:- L19 – L20
#include <bits/stdc++.h>
#include <cmath>
using namespace std;
int main(){
vector<int>v1;
vector<int>v2;
int cap;
cin>>cap;
int n;
cin>>n;
int l;
for(int i=0;i<n;i++){
cin>>l;
v1.push_back(l);
}
for(int i=0;i<n;i++){
cin>>l;
v2.push_back(l);
}
vector<int>v3;
for(int i=0;i<n;i++){
v3.push_back(v2[i]/v1[i]);

int p=0;
float s;
sort(v3.begin(),v3.end());
reverse(v3.begin(),v3.end());

for(int i=0;i<n;i++){
if(cap>0 && v1[i]<=cap){
cap=cap-v1[i];
p+=v2[i];

cout<<"1.0 ";
}
else if(cap>0){

s=((float)cap/(float)v1[i]);
cout<<round(s*10)/10<<" ";
p+=v2[i]*s;

}
else{

cout<<"0.0 ";
break;
}
}

}
#include <iostream>

using namespace std;

void activitySelection(int start[], int finish[], int n, int k) {

int i = 0, j, count = 0;

cout << i << " ";


count++;

for (j = 1; j < n; j++) {

if (start[j] >= finish[i]) {

cout << j << " ";

count++;

i = j;

if (count == k) {

break;

int main() {
int n, k;

cin >> n;

cin >> k;

int start[n], finish[n];

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

cin >> start[i];

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

cin >> finish[i];

activitySelection(start, finish, n, k);

return 0;

}
#include <iostream>

using namespace std;

struct PlayerInfo {

int winnerIndex;

int worstIndex;

};
PlayerInfo findWinnerAndWorst(int scores[], int low, int high) {

if (low == high) {

return {low, low}; // Use direct initialization for structs in C++

int mid = (low + high) / 2;

PlayerInfo left = findWinnerAndWorst(scores, low, mid);

PlayerInfo right = findWinnerAndWorst(scores, mid + 1, high);

PlayerInfo result;

result.winnerIndex = scores[left.winnerIndex] > scores[right.winnerIndex] ? left.winnerIndex :


right.winnerIndex;

result.worstIndex = scores[left.worstIndex] < scores[right.worstIndex] ? left.worstIndex :


right.worstIndex;

return result;

int main() {
int n;

cin >> n;

int scores[n];

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

cin >> scores[i];

PlayerInfo result = findWinnerAndWorst(scores, 0, n - 1);

cout << "Winner " << result.winnerIndex + 1 << endl;

cout << "Worst player " << result.worstIndex + 1 << endl;

return 0;

}
#include <stdio.h>

int min(int a, int b) {

return (a < b) ? a : b;

}
int carAssembly(int a[2][4], int t[2][3], int e[2], int x[2], int n) {

int T1[n], T2[n]; // Tables to store minimum time to reach station j

// Base cases: Entry time for the first station

T1[0] = e[0] + a[0][0];

T2[0] = e[1] + a[1][0];

// Build the tables T1[] and T2[] using bottom-up approach

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

T1[j] = min(T1[j - 1] + a[0][j], T2[j - 1] + t[1][j - 1] + a[0][j]);

T2[j] = min(T2[j - 1] + a[1][j], T1[j - 1] + t[0][j - 1] + a[1][j]);

// Minimum time to build the car is the minimum of exit times from the two lines

return min(T1[n - 1] + x[0], T2[n - 1] + x[1]);

}
int main() {

int n;

scanf("%d", &n);

int a[2][4], t[2][3], e[2], x[2];

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

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

scanf("%d", &a[i][j]);

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

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

scanf("%d", &t[i][j]);
}

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

scanf("%d", &e[i]);

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

scanf("%d", &x[i]);

int minimumTime = carAssembly(a, t, e, x, n);

printf("%d\n", minimumTime);

return 0;

}
#include <iostream>

#include <vector>

using namespace std;

// Function to find the longest common subsequence

string longestCommonSubsequence(string s1, string s2) {

int m = s1.length();

int n = s2.length();
// Create a 2D vector to store the lengths of the longest common subsequences

vector<vector<int>> dp(m + 1, vector<int>(n + 1, 0));

// Fill up the dp table

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

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

if (s1[i - 1] == s2[j - 1]) {

dp[i][j] = dp[i - 1][j - 1] + 1;

} else {

dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]);

// Build the longest common subsequence

string lcs;

int i = m, j = n;

while (i > 0 && j > 0) {

if (s1[i - 1] == s2[j - 1]) {

lcs = s1[i - 1] + lcs;

i--;

j--;

} else if (dp[i - 1][j] > dp[i][j - 1]) {

i--;
} else {

j--;

return lcs;

int main() {

string s1, s2;

//cout << "Enter the length of sequence 1: ";

int len1;

cin >> len1;

//cout << "Enter sequence 1: ";

//cout << "Enter the length of sequence 2: ";

int len2;

cin >> len2;

//cout << "Enter sequence 2: ";

cin >> s1;

cin >> s2;

string lcs = longestCommonSubsequence(s1, s2);

cout << lcs << endl;


return 0;

#include <iostream>

#include <vector>
using namespace std;

// Function to find the longest common subsequence

string longestCommonSubsequence(string s1, string s2) {

int m = s1.length();

int n = s2.length();

// Create a 2D vector to store the lengths of the longest common subsequences

vector<vector<int>> dp(m + 1, vector<int>(n + 1, 0));

// Fill up the dp table

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

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

if (s1[i - 1] == s2[j - 1]) {

dp[i][j] = dp[i - 1][j - 1] + 1;

} else {

dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]);

// Build the longest common subsequence

string lcs;

int i = m, j = n;

while (i > 0 && j > 0) {


if (s1[i - 1] == s2[j - 1]) {

lcs = s1[i - 1] + lcs;

i--;

j--;

} else if (dp[i - 1][j] > dp[i][j - 1]) {

i--;

} else {

j--;

return lcs;

int main() {

string s1, s2;

//cout << "Enter the length of sequence 1: ";

int len1;

cin >> len1;

//cout << "Enter sequence 1: ";

//cout << "Enter the length of sequence 2: ";

int len2;

cin >> len2;


//cout << "Enter sequence 2: ";

cin >> s1;

cin >> s2;

string lcs = longestCommonSubsequence(s1, s2);

cout << lcs << endl;

return 0;

}
#include <iostream>

#include <vector>

using namespace std;

// Function to solve the knapsack problem

void knapsack(int n, int W, const vector<int>& weights, const vector<int>& profits) {

vector<vector<int>> K(n + 1, vector<int>(W + 1, 0));

vector<vector<int>> keep(n + 1, vector<int>(W + 1, 0));

// Build table K[][] in bottom-up manner

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

for (int w = 0; w <= W; w++) {


if (i == 0 || w == 0)

K[i][w] = 0;

else if (weights[i - 1] <= w) {

K[i][w] = max(profits[i - 1] + K[i - 1][w - weights[i - 1]], K[i - 1][w]);

if (profits[i - 1] + K[i - 1][w - weights[i - 1]] > K[i - 1][w])

keep[i][w] = 1;

else

keep[i][w] = 0;

} else {

K[i][w] = K[i - 1][w];

keep[i][w] = 0;

// Find items to be selected

vector<int> selectedItems(n, 0);

int i = n, w = W;

while (i > 0 && w > 0) {

if (keep[i][w] == 1) {

selectedItems[i - 1] = 1;

w -= weights[i - 1];

i--;

}
// Print the solution

//cout << "Output: ";

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

cout << selectedItems[i] << " ";

cout << endl;

int main() {

int n, W;

//cout << "Enter the number of items: ";

cin >> n;

//cout << "Enter the knapsack capacity: ";

cin >> W;

vector<int> weights(n), profits(n);

//cout << "Enter the weights of items: ";

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

cin >> weights[i];

//cout << "Enter the profits of items: ";

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


cin >> profits[i];

knapsack(n, W, weights, profits);

return 0;

#include <iostream>
#include <vector>

using namespace std;

bool isSafe(const vector<vector<int>>& board, int row, int col, int n) {

// Check if there is a queen in the same column

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

if (board[i][col] == 1) {

return false;

// Check if there is a queen in the upper-left diagonal

for (int i = row, j = col; i >= 0 && j >= 0; --i, --j) {

if (board[i][j] == 1) {

return false;

// Check if there is a queen in the upper-right diagonal

for (int i = row, j = col; i >= 0 && j < n; --i, ++j) {

if (board[i][j] == 1) {

return false;

return true;

bool solveNQueensUtil(vector<vector<int>>& board, int row, int n) {

if (row == n) {
// All queens are placed successfully

return true;

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

if (isSafe(board, row, col, n)) {

// Place queen and check for the next row

board[row][col] = 1;

if (solveNQueensUtil(board, row + 1, n)) {

return true; // Solution found

// If placing queen in the current position doesn't lead to a solution, backtrack

board[row][col] = 0;

return false; // No safe position found for this row

void solveNQueens(int n) {

vector<vector<int>> board(n, vector<int>(n, 0));

if (!solveNQueensUtil(board, 0, n)) {

cout << "No solution exists for N = " << n << endl;

return;

// Print the solution

for (int i = n-1; i >= 0; --i) {

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


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

cout << endl;

int main() {

int n;

cin >> n;

solveNQueens(n);

return 0;

}
#include <iostream>

#include <climits>

using namespace std;

int max_crossing_subarray(int arr[], int low, int mid, int high, int& max_left, int&
max_right) {

int left_sum = INT_MIN;

int sum_temp = 0;

max_left = mid;

for (int i = mid; i >= low; --i) {

sum_temp += arr[i];

if (sum_temp > left_sum) {

left_sum = sum_temp;

max_left = i;

}
int right_sum = INT_MIN;

sum_temp = 0;

max_right = mid + 1;

for (int i = mid + 1; i <= high; ++i) {

sum_temp += arr[i];

if (sum_temp > right_sum) {

right_sum = sum_temp;

max_right = i;

return left_sum + right_sum;

int max_subarray(int arr[], int low, int high, int& max_left, int& max_right) {

if (low == high) {

max_left = low;

max_right = high;

return arr[low];

int mid = (low + high) / 2;


int left_low, left_high, left_sum;

int right_low, right_high, right_sum;

int cross_low, cross_high, cross_sum;

left_sum = max_subarray(arr, low, mid, left_low, left_high);

right_sum = max_subarray(arr, mid + 1, high, right_low, right_high);

cross_sum = max_crossing_subarray(arr, low, mid, high, cross_low, cross_high);

if (left_sum >= right_sum && left_sum >= cross_sum) {

max_left = left_low;

max_right = left_high;

return left_sum;

} else if (right_sum >= left_sum && right_sum >= cross_sum) {

max_left = right_low;

max_right = right_high;

return right_sum;

} else {

max_left = cross_low;

max_right = cross_high;

return cross_sum;

int main() {

int a;
cin>>a;

int arr[a] ;

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

cin>>arr[i];

int max_left, max_right;

int result = max_subarray(arr, 0, a - 1, max_left, max_right);

cout<<"Length of the subarray: "<<(max_right - max_left + 1) << endl;

cout << "Maximum sum:" << result << endl;

for (int i = max_left; i <= max_right; ++i) {

cout << arr[i];

if (i < max_right) cout << " ";

return 0;

}
#include <iostream>

#include <cmath>

using namespace std;

// Function to calculate the number of digits in an integer

int getNumDigits(int num) {

return (num == 0) ? 1 : static_cast<int>(log10(abs(num)) + 1);

// Function to multiply two integers using Karatsuba algorithm

long long karatsuba(int x, int y) {

// Base case: if either x or y is a single-digit number


if (getNumDigits(x) == 1 || getNumDigits(y) == 1) {

return static_cast<long long>(x) * y;

// Calculate the size of the numbers

int n = max(getNumDigits(x), getNumDigits(y));

// Calculate the midpoint

int m = n / 2;

// Split the numbers at the midpoint

int high1 = x / static_cast<int>(pow(10, m));

int low1 = x % static_cast<int>(pow(10, m));

int high2 = y / static_cast<int>(pow(10, m));

int low2 = y % static_cast<int>(pow(10, m));

// Recursive steps

long long z0 = karatsuba(low1, low2);

long long z1 = karatsuba((low1 + high1), (low2 + high2));

long long z2 = karatsuba(high1, high2);

// Calculate the final result using the Karatsuba formula

return (z2 * static_cast<long long>(pow(10, 2 * m))) +

((z1 - z2 - z0) * static_cast<long long>(pow(10, m))) + z0;

int main() {

int x, y;

cin >> x;

cin >> y;
// Calculate and print the product using Karatsuba algorithm

long long result = karatsuba(x, y);

cout << result << endl;

return 0;

You might also like