Ilovepdf Merged
Ilovepdf Merged
0801IT221131
Design and Analysis of Algorithms
Assignment 1
Code:
#include <iostream>
#include <vector>
#include <algorithm>
#include <iomanip>
#include <cmath>
int main() {
vector<Student> students = {
{1, 360, 280, 6.4},
{2, 415, 345, 7.3},
{3, 280, 247, 5.9},
{4, 312, 276, 6.2},
{5, 296, 310, 8.1}
};
return 0;
}
Tanusha Choudhary
0801IT221131
Design and Analysis of Algorithms
Assignment 2
Problem: Write an algorithm to search a maximum element ‘M’ from a list ‘A’. Assume the list
is already sorted in ascending/descending order, try to implement using the concept of either
linear or binary search algorithm.
Input Format
Input:First takes the size of a list as input from user and then Take the values of elements from
the user. At the end return the Maximum Value.
Sample input-0:
A [10, 20, 30, 40, 50]
Sample input-1:
A [45, 37, 29, 22, 18, 17, 13, 9, 5, 3]
Sample input-2:
A [8]
Sample input-3:
A[]
Constraints
1. ( 2 <= A <=1024 )- i.e. No. of elements in a list.
2. ( M ∈ A )- i.e. maximum element always belongs to list A.
3. Return the same Element if there is only Element in the list
Output Format
Output: Return the maximum valued element from the list.
Sample output-0: [based on the sample input-0] 50
Sample output-1: [based on the sample input-1] 45
Sample output-2: [based on the sample input-2] 8
Sample output-3: [based on the sample input-3] -1
Sample Input 0
5
10
20
30
40
50
Sample Output 0
50
Sample Input 1
1
0
Sample Output 1
0
Tanusha Choudhary
0801IT221131
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin>>n;
vector<int> a(n);
for(int i=0;i<n;i++){
cin>>a[i];
}
int maxi=INT_MIN;
for(int i=0;i<n;i++){
if(a[i]>maxi) maxi=a[i];
}
cout<<maxi;
return 0;
}
Tanusha Choudhary
0801IT221131
Design and Analysis of Algorithms
Assignment 3
Q.1 Given a list of numbers. Write an efficient algorithm to find kth smallest element from
the list of numbers without applying sorting. Value of ‘k’ is to be taken as input from the
user.
#include <stdio.h>
void swap(int *a, int *b) {
int temp = *a;
*a = *b;
*b = temp;
}
int partition(int arr[], int low, int high) {
int pivot = arr[high];
int i = (low - 1);
for (int j = low; j <= high - 1; j++) {
if (arr[j] <= pivot) {
i++;
swap(&arr[i], &arr[j]);
}
}
swap(&arr[i + 1], &arr[high]);
return (i + 1);
}
int quickselect(int arr[], int low, int high, int k) {
if (low <= high) {
int pivotIndex = partition(arr, low, high);
if (pivotIndex == k - 1) {
return arr[pivotIndex];
} else if (pivotIndex > k - 1) {
return quickselect(arr, low, pivotIndex - 1, k);
} else {
return quickselect(arr, pivotIndex + 1, high, k);
}
}
return -1;
}
int main() {
int arr[] = {7, 10, 4, 3, 20, 15};
Tanusha Choudhary
0801IT221131
int n = sizeof(arr)/sizeof(arr[0]);
int k;
printf("Enter the value of k: ");
scanf("%d", &k);
if (k > 0 && k <= n) {
printf("The %d-th smallest element is %d\n", k, quickselect(arr, 0, n - 1, k));
} else {
printf("Invalid input for k\n");
}
return 0;
}
Q.2 Given an array consisting of total 2m elements as per the following format: { x1, x2, x3,
x4, ….., xm, y1, y2, y3, y4, …., ym }. You are required to write an efficient C program to
shuffle the array to {x1, y1, x2, y2, x3, y3, ……, xm, ym } without using extra space. (You
can use one temp variable at max for swapping operation)
#include <stdio.h>
void rotate(int arr[], int start, int mid, int end) {
int temp = arr[mid];
for (int i = mid; i > start; i--) {
arr[i] = arr[i - 1];
}
arr[start] = temp;
}
void shuffleArray(int arr[], int start, int end) {
if (end - start <= 1) return;
int mid = (start + end) / 2;
int leftMid = (start + mid) / 2;
for (int i = leftMid + 1; i <= mid; i++) {
rotate(arr, start + i - leftMid - 1, i, end);
}
shuffleArray(arr, start, mid);
shuffleArray(arr, mid + 1, end);
}
Tanusha Choudhary
0801IT221131
int main() {
int arr[] = {1, 2, 3, 4, 5, 6, 7, 8};
int n = sizeof(arr) / sizeof(arr[0]);
shuffleArray(arr, 0, n - 1);
printf("Shuffled Array:\n");
for (int i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
printf("\n");
return 0;
}
Q.3 Given two sorted arrays of size m and n respectively, you are required to find the
element that would be at the kth final position in the sorted array.
#include <stdio.h>
int findKth(int arr1[], int m, int arr2[], int n, int k) {
if (m > n) {
return findKth(arr2, n, arr1, m, k);
}
if (m == 0) {
return arr2[k - 1];
}
if (k == 1) {
return arr1[0] < arr2[0] ? arr1[0] : arr2[0];
}
int i = (k / 2 < m) ? k / 2 : m;
int j = (k / 2 < n) ? k / 2 : n;
if (arr1[i - 1] > arr2[j - 1]) {
return findKth(arr1, m, arr2 + j, n - j, k - j);
} else {
return findKth(arr1 + i, m - i, arr2, n, k - i);
}
}
int main() {
Tanusha Choudhary
0801IT221131
int arr1[] = {2, 3, 6, 7, 9};
int arr2[] = {1, 4, 8, 10};
int m = sizeof(arr1) / sizeof(arr1[0]);
int n = sizeof(arr2) / sizeof(arr2[0]);
int k;
printf("Enter the value of k: ");
scanf("%d", &k);
if (k > 0 && k <= (m + n)) {
printf("The %d-th element in the merged sorted array is %d\n", k, findKth(arr1, m, arr2, n,
k));
} else {
printf("Invalid input for k\n");
}
return 0;
}
Q.4 We have to color ‘m’ boards of length {L1, L2, .. Lm}. There are ‘k’ painters available
and each takes 1 unit time to paint 1 unit of board. The problem is to find the minimum
time to get this job done under the constraints that any painter will only paint continuous
sections of boards, say board {5, 6, 7} or only board {2} or nothing but not board {3, 7, 8}
#include <stdio.h>
#include <limits.h>
int max(int a, int b) {
return (a > b) ? a : b;
}
int isPossible(int boards[], int n, int k, int maxTime) {
int paintersRequired = 1;
int currentLength = 0;
for (int i = 0; i < n; i++) {
if (currentLength + boards[i] > maxTime) {
paintersRequired++;
currentLength = boards[i];
if (paintersRequired > k) {
return 0;
Tanusha Choudhary
0801IT221131
}
} else {
currentLength += boards[i];
}
}
return 1;
}
int findMinTime(int boards[], int n, int k) {
int sum = 0;
int maxBoard = INT_MIN;
for (int i = 0; i < n; i++) {
sum += boards[i];
maxBoard = max(maxBoard, boards[i]);
}
int low = maxBoard;
int high = sum;
int result = high;
while (low <= high) {
int mid = (low + high) / 2;
if (isPossible(boards, n, k, mid)) {
result = mid;
high = mid - 1;
} else {
low = mid + 1;
}
}
return result;
}
int main() {
int boards1[] = {5, 5, 5, 5};
int k1 = 2;
int n1 = sizeof(boards1) / sizeof(boards1[0]);
printf("Minimum time to paint boards: %d\n", findMinTime(boards1, n1, k1));
int boards2[] = {5, 10, 15, 20, 25};
int k2 = 2;
int n2 = sizeof(boards2) / sizeof(boards2[0]);
printf("Minimum time to paint boards: %d\n", findMinTime(boards2, n2, k2));
return 0; }
Tanusha Choudhary
0801IT221131
Design and Analysis of Algorithms
Assignment 4
Problem Definition
• Input: A list of products, where each product has a price attribute.
• Objective: Sort the list of products by price in ascending order.
• Constraints:
o The sorting algorithm should be efficient for large datasets.
o The solution should work within the platform’s memory constraints.
o The sorting operation should be performed in reasonable time to ensure quick page load times
for users.
Code:
#include<stdio.h>
typedef struct{
char name[50];
int price;
}Product;
int main(){
Product products[]={
{"Laptop",999},
{"Smartphone",699},
{"Tablet",399},
{"Headphones",149},
{"Smartwatch",199}
};
int n=sizeof(products) / sizeof(products[0]);
printf("Originalist:\n");
printProducts(products,n);
quickSort(products,0,n-1);
printf("\nSortedlist:\n");
printProducts(products,n);
return0;
}