You are on page 1of 13

ASSIGNMENT NUMBER: 9

Name :Mridul KANTI SIKDER


Roll no:21CS8002
Subject :Design analysis and algorithm

PART 1

#include <stdio.h>
#include <limits.h>
#include <math.h>

// Function to implement single transaction using divide &


conquer

int singletrans1(int B[], int S[], int n, int C)


{
// Base case: if there are fewer than 2 days, there will
be no transaction can
if (n < 2)
{
return 0;
}

// devide the days into two halves


int mid = n / 2;

// Find the minimum buying rate and its corresponding day


in the left half
int min_buy_rate_left = INT_MAX;
int min_buy_day_left = -1;
int total = 0;
for (int i = 0; i < mid; i++)
{

if (B[i] < min_buy_rate_left)


{
min_buy_rate_left = B[i];
min_buy_day_left = i;
total = C / B[i];

}
}

// Find the maximum selling rate and corresponding day in


right half
int max_sell_rate_right = 0;
int max_sell_day_right = -1;
for (int j = mid; j < n; j++)
{
if (S[j] > max_sell_rate_right)
{
max_sell_rate_right = S[j];
max_sell_day_right = j;
}
}

// Calculate profit if buying in the left half and selling


in the right half

int profit_left_right = total * (max_sell_rate_right -


min_buy_rate_left);

// Recursively compute profits for left and right halves

int profit_left = singletrans1(B, S, mid, C);


int profit_right = singletrans1(B + mid, S + mid, n - mid,
C);

// Maximum profit among all possibilities


int max_profit = (profit_left > profit_right) ?
profit_left : profit_right;
if (profit_left_right > max_profit)
{
max_profit = profit_left_right;
}
return max_profit;
}

// Function to implement single transaction using divide and


conquer with O(n) time complexity

int singletrans2(int B[], int S[], int n, int C)


{

// Initialize variables to keep track of minimum buy rate,


maximum sell rate, and maximum profit

int mini = INT_MAX;


int profit = 0;
int no = 0;
int bi = 0, si = 0;
for (int i = 0; i < n; i++)
{

if (mini > B[i])


{
no = C / B[i];
mini = B[i];
bi = i;
}

// profit=max(profit, no*(s[i]- mini));

if (profit < no * (S[i] - mini))


{
profit = no * (S[i] - mini);
si = i;
}
}
printf("Buying date: %d\n", bi);
printf("Selling date: %d\n", si);

printf("Max Profit: %d ", profit);


}

int main()
{
int B[] = {10, 15, 16, 9, 25, 6, 5, 18, 5, 21};
int S[] = {24, 13, 8, 12, 9, 21, 7, 21, 6, 14};
int n = sizeof(B) / sizeof(B[0]);
printf("\n");
printf("n = %d \n", n);

printf("Buying Prices:\n");
for (int i = 0; i < n; i++)
{
printf("%d ", B[i]);
}
printf("\n");

printf("Selling Prices:\n");
for (int i = 0; i < n; i++)
{
printf("%d ", S[i]);
}
printf("\n");

int C = 1000;
// Initial capital

// int profit1 = singletrans1(B, S, n, C);

printf("\n");
printf("Method 1\n");
printf("Max Profit: %d", singletrans1(B, S, n, C));

printf("\n");
printf("\n");
printf("Method 2\n");
singletrans2(B, S, n, C);
printf("\n");
printf("\n");

return 0;
}

Output:

PS C:\Users\HP\daa lab> cd "c:\Users\HP\daa lab\" ; if ($?) { gcc


daa9_part1.c -o daa9_part1 } ; if ($?) { .\daa9_part1 }

n = 10
Buying Prices:
10 15 16 9 25 6 5 18 5 21
Selling Prices:
24 13 8 12 9 21 7 21 6 14

Method 1
Max Profit: 3200

Method 2
Buying date: 6
Selling date: 7
Max Profit: 3200
Part 2

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

// Structure to represent a transaction


struct Transaction
{
int buy_date;
int sell_date;
int buy_rate;
int sell_rate;
int profit;
};

// Function to implement single transaction with O(n log n)


time complexity
void singletrans(int B[], int S[], int n, int C)
{
int max_profit = 0;
int buy_date = 0, sell_date = 0;

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


{
int max_sell_price = S[i];
int sell_date_tmp = i; // Temporary variable to store
sell date

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


{
if (S[j] > max_sell_price)
{
max_sell_price = S[j];
sell_date_tmp = j;
}
}
int max_possible_buy = C / B[i];
int max_possible_profit = max_possible_buy *
(max_sell_price - B[i]);

if (max_possible_profit > max_profit)


{
max_profit = max_possible_profit;
buy_date = i;
sell_date = sell_date_tmp;
}
}

printf("***Single transaction: O(n log n) time\n");


printf("Buying date = %d, Selling date = %d, Buying rate =
%d, Selling rate = %d\n", buy_date, sell_date, B[buy_date],
S[sell_date]);
printf("Maximum profit = %d\n", max_profit);
}

// Function to implement single transaction with O(n) time


complexity
void singletrans2(int B[], int S[], int n, int C)
{
int max_profit = 0;
int buy_date = 0, sell_date = 0;
int min_buy_price = B[0];

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


{
int max_possible_buy = C / B[i];
int max_possible_profit = max_possible_buy * (S[i] -
B[i]);

if (max_possible_profit > max_profit)


{
max_profit = max_possible_profit;
buy_date = i;
sell_date = i;
}

if (B[i] < min_buy_price)


{
min_buy_price = B[i];
buy_date = i;
}
else if (S[i] > S[sell_date])
{
sell_date = i;
}
}

printf("***Single transaction: O(n) time\n");


printf("Buying date = %d, Selling date = %d, Buying rate =
%d, Selling rate = %d\n", buy_date, sell_date, B[buy_date],
S[sell_date]);
printf("Maximum profit = %d\n", max_profit);
}

// Function to implement multiple transactions


void multitrans(int B[], int S[], int n, int C)
{
struct Transaction transactions[n];
int capital = C;
int num_transactions = 0;

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


{
int max_possible_buy = capital / B[i];
int max_sell_price = -1;
int sell_date = -1;

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


{
if (S[j] > max_sell_price)
{
max_sell_price = S[j];
sell_date = j;
}
}

if (sell_date != -1)
{
int profit = max_possible_buy * (max_sell_price -
B[i]);
if (profit > 0)
{
struct Transaction t;
t.buy_date = i;
t.sell_date = sell_date;
t.buy_rate = B[i];
t.sell_rate = S[sell_date];
t.profit = profit;
transactions[num_transactions] = t;
num_transactions++;
capital += profit;
}
}
}

printf("***Multiple transactions\n");
printf("Initial Capital = %d\n", C);

int total_profit = 0;
int sell_date = -1;

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


{
printf("Buying date %d, Buying rate %d\n",
transactions[i].buy_date, transactions[i].buy_rate);
printf("Selling date %d, Selling rate %d\n",
transactions[i].sell_date, transactions[i].sell_rate);
printf("Current capital %d\n", capital);

total_profit += transactions[i].profit;
sell_date = transactions[i].sell_date;
}

printf("Maximum profit = %d\n", total_profit);


}

int main()
{
int n, C;
printf("Enter n = ");
scanf("%d", &n);

int B[n], S[n];

printf("Buying prices: ");


for (int i = 0; i < n; i++)
{
scanf("%d", &B[i]);
}

printf("Selling prices: ");


for (int i = 0; i < n; i++)
{
scanf("%d", &S[i]);
}

printf("Enter C = ");
scanf("%d", &C);

singletrans(B, S, n, C);
singletrans2(B, S, n, C);
multitrans(B, S, n, C);

return 0;
}

Output:
cd "c:\Users\HP\daa lab\" ; if ($?) { gcc daa9_part2.c -o daa9_part2 } ; if
($?) { .\daa9_part2 }

Enter n = 10
Buying prices: 400
500
500
555
555
785
855
87
5
56
Selling prices: 455
455
855
988
5
55
5
5
5
666
Enter C = 120

***Single transaction: O(n log n) time

Buying date = 8, Selling date = 9, Buying rate = 5, Selling rate = 666


Maximum profit = 15864

***Single transaction: O(n) time


Buying date = 9, Selling date = 9, Buying rate = 56, Selling rate = 666
Maximum profit = 1220
***Multiple transactions

Initial Capital = 120


Buying date 7, Buying rate 87
Selling date 9, Selling rate 666
Current capital 92578
Buying date 8, Buying rate 5
Selling date 9, Selling rate 666
Current capital 92578

You might also like