0% found this document useful (0 votes)
2 views

algorithm lab assignment 3

The document contains a series of C programming code snippets that demonstrate various algorithms and data structures. These include finding the maximum of three numbers, calculating the sum of first and last digits, removing duplicates from an array, and implementing a fractional knapsack problem using a greedy approach. Additionally, it covers dynamic programming solutions such as finding the minimum number of notes for a withdrawal.

Uploaded by

shahriarmonon
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

algorithm lab assignment 3

The document contains a series of C programming code snippets that demonstrate various algorithms and data structures. These include finding the maximum of three numbers, calculating the sum of first and last digits, removing duplicates from an array, and implementing a fractional knapsack problem using a greedy approach. Additionally, it covers dynamic programming solutions such as finding the minimum number of notes for a withdrawal.

Uploaded by

shahriarmonon
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 18

1.

#include <stdio.h>

int main() {

int num1, num2, num3;

printf("Input num1: ");

scanf("%d", &num1);

printf("Input num2: ");

scanf("%d", &num2);

printf("Input num3: ");

scanf("%d", &num3);

int max;

if (num1 >= num2 && num1 >= num3)

max = num1;

else if (num2 >= num1 && num2 >= num3)

max = num2;

else

max = num3;

printf("Maximum is: %d\n", max);

return 0;

}
2.

#include <stdio.h>

int main() {

int num, first, last;

printf("Input number: ");

scanf("%d", &num);

last = num % 10;

while (num >= 10)

num /= 10;

first = num;

printf("Sum of first and last digit: %d\n", first + last);

return 0;

3.

#include <stdio.h>

int main() {

int arr[] = {10, 20, 10, 1, 100, 10, 2, 1, 5, 10};

int n = sizeof(arr) / sizeof(arr[0]);


int unique[n], size = 0;

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

int found = 0;

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

if (arr[i] == unique[j]) {

found = 1;

break;

if (!found)

unique[size++] = arr[i];

printf("After removing duplicates:\n");

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

printf("%d ", unique[i]);

return 0;

}
4.

#include <stdio.h>

int main() {

int n, k;

printf("Enter N: ");

scanf("%d", &n);

int arr[n];

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

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

printf("Enter K: ");

scanf("%d", &k);

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

if (arr[i] == k) {

printf("Position: %d\n", i);

return 0;

printf("-1\n");

return 0;
}

5.

#include <stdio.h>

int findMissing(int arr[], int n) {

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

if (arr[i] != i)

return i;

return n;

int main() {

int arr[] = {0, 1, 2, 6, 9, 11, 15};

int n = sizeof(arr) / sizeof(arr[0]);

printf("The smallest missing element is %d\n", findMissing(arr, n));

return 0;

6.

#include <stdio.h>
int main() {

int arr[] = {10, 20, 4, 45, 99};

int n = sizeof(arr) / sizeof(arr[0]);

int largest = arr[0], second = -1;

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

if (arr[i] > largest) {

second = largest;

largest = arr[i];

} else if (arr[i] > second && arr[i] != largest) {

second = arr[i];

printf("Second largest element: %d\n", second);

return 0;

7.

#include <stdio.h>

void rotateLeft(int arr[], int n, int d) {

int temp[d];

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


temp[i] = arr[i];

for (int i = 0; i < n - d; i++)

arr[i] = arr[i + d];

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

arr[n - d + i] = temp[i];

int main() {

int arr[] = {1, 2, 3, 4, 5};

int n = sizeof(arr) / sizeof(arr[0]);

int d = 2;

rotateLeft(arr, n, d);

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

printf("%d ", arr[i]);

return 0;

8.

#include <stdio.h>
int main() {

int arr[] = {1, 2, 2, 3, 4, 1, 3};

int n = sizeof(arr) / sizeof(arr[0]);

int visited[n];

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

if (visited[i])

continue;

int count = 1;

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

if (arr[i] == arr[j]) {

count++;

visited[j] = 1;

printf("%d: %d times\n", arr[i], count);

return 0;

9.

#include <stdio.h>
int main() {

int arr[] = {1, 2, 3, 4, 5};

int target = 5, n = sizeof(arr) / sizeof(arr[0]);

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

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

if (arr[i] + arr[j] == target)

printf("(%d, %d)\n", arr[i], arr[j]);

return 0;

10.

#include <stdio.h>

int maxDifference(int arr[], int n) { int min_val = arr[0], max_diff = arr[1] - arr[0];

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


if (arr[i] - min_val > max_diff)
max_diff = arr[i] - min_val;

if (arr[i] < min_val)


min_val = arr[i];
}
return max_diff;

int main() { int arr[] = {2, 3, 10, 6, 4, 8, 1}; int n = sizeof(arr) / sizeof(arr[0]);
printf("Maximum Difference: %d\n", maxDifference(arr, n));
return 0;

11.

#include <stdio.h>

#include <string.h>

void findDuplicates(char str[]) {

int freq[256] = {0}; // ASCII character frequency array

for (int i = 0; str[i] != '\0'; i++)

freq[(int)str[i]]++;

printf("Duplicate characters: ");

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

if (freq[i] > 1)

printf("%c ", i);

int main() {
char str[] = "programming";

findDuplicates(str);

return 0;

12.

#include <stdio.h>

#include <string.h>

void findLongestWord(char str[]) {

char *token = strtok(str, " ");

char *longest = token;

while (token != NULL) {

if (strlen(token) > strlen(longest))

longest = token;

token = strtok(NULL, " ");

printf("Longest word: %s\n", longest);

}
int main() {

char str[] = "I love programming challenges";

findLongestWord(str);

return 0;

13.

#include <stdio.h>

#include <string.h>

void replaceCharacter(char str[], char oldChar, char newChar) {

for (int i = 0; str[i] != '\0'; i++) {

if (str[i] == oldChar)

str[i] = newChar;

int main() {

char str[] = "hello world";

replaceCharacter(str, 'l', 'x');

printf("Modified string: %s\n", str);

return 0;

}
14.

#include <stdio.h>

int isValidSplit(int arr[], int n, int m, int maxSum) {

int currentSum = 0, count = 1;

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

if (arr[i] > maxSum) return 0;

if (currentSum + arr[i] > maxSum) {

count++;

currentSum = arr[i];

if (count > m) return 0;

} else {

currentSum += arr[i];

return 1;

int splitArray(int arr[], int n, int m) {

int low = 0, high = 0, result = 0;

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

high += arr[i];
while (low <= high) {

int mid = (low + high) / 2;

if (isValidSplit(arr, n, m, mid)) {

result = mid;

high = mid - 1;

} else {

low = mid + 1;

return result;

int main() {

int arr[] = {7, 2, 5, 10, 8};

int n = sizeof(arr) / sizeof(arr[0]), m = 2;

printf("Minimum Largest Sum: %d\n", splitArray(arr, n, m));

return 0;

15.

#include <stdio.h>

#include <stdlib.h>

int findClosest(int arr[], int n, int target) {


int closest = arr[0], minDiff = abs(target - arr[0]);

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

int diff = abs(target - arr[i]);

if (diff < minDiff) {

minDiff = diff;

closest = arr[i];

return closest;

int main() {

int arr[] = {1, 3, 5, 7, 9};

int n = sizeof(arr) / sizeof(arr[0]), target = 6;

printf("Closest number: %d\n", findClosest(arr, n, target));

return 0;

Greedy Approach Solutions

Problem 1: Maximum Value in Fractional Knapsack

c
CopyEdit
#include <stdio.h>
#include <stdlib.h>

typedef struct {
int weight, value;
double ratio;
} Item;

int cmp(const void *a, const void *b) {


return ((Item *)b)->ratio - ((Item *)a)->ratio;
}

double fractionalKnapsack(int weights[], int values[], int n, int W) {


Item items[n];
for (int i = 0; i < n; i++) {
items[i].weight = weights[i];
items[i].value = values[i];
items[i].ratio = (double)values[i] / weights[i];
}

qsort(items, n, sizeof(Item), cmp);

double maxValue = 0;
for (int i = 0; i < n && W > 0; i++) {
if (items[i].weight <= W) {
maxValue += items[i].value;
W -= items[i].weight;
} else {
maxValue += items[i].ratio * W;
break;
}
}
return maxValue;
}

int main() {
int n = 3, W = 50;
int weights[] = {10, 20, 30}, values[] = {60, 100, 120};
printf("Maximum Value: %.2f\n", fractionalKnapsack(weights,
values, n, W));
return 0;
}
Output: 240.00

Dynamic Programming Solutions

Problem 01: Minimum Notes for Withdrawal

c
CopyEdit
#include <stdio.h>

int minNotes(int n) {
int notes[] = {100, 50, 25, 20, 10, 5, 2}, count = 0;
for (int i = 0; i < 7; i++) {
count += n / notes[i];
n %= notes[i];
}
return n == 0 ? count : -1;
}

int main() {
int n;
scanf("%d", &n);

int result = minNotes(n);


if (result == -1)
printf("Sorry Sir! Enter Again\n");
else
printf("%d\n", result);

return 0;
}
Sample Input: 105
Output: 2

You might also like