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

AP LAB -8TH merged using functions

Uploaded by

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

AP LAB -8TH merged using functions

Uploaded by

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

DEPARTMENT OF

COMPUTER SCIENCE & ENGINEERING

Experiment 8 A

Student Name: NEMESIS UID: 22BAI


Branch: CSE Section/Group:
Semester: 5 Date of Performance:
Subject Name: DAA Lab Subject Code: 22CSH-311

1. TITLE:
Marc's Cakewalk
2. AIM:
Problem Marc loves cupcakes, but he also likes to stay fit. Each cupcake has a
calorie count, and Marc can walk a distance to expend those calories. If Marc has
eaten j cupcakes so far, after eating a cupcake with ccc calories he must walk at least
2j×c miles to maintain his weight.

3. Objective
Print Complete the marcsCakewalk function in the editor below.
marcsCakewalk has the following parameters(s):
int calorie[n]: the calorie counts for each cupcake

4. Algorithm

1. Accept the number of calorie values n and input each calorie into a fixed-size array.
2. Sort the array of calorie values in descending order to maximize the total miles.
3. Sort Iterate through the sorted array, computing the total miles by multiplying each
calorie value by a power of two (specifically, 2 for each index i).
4. Sum up the miles for each calorie value to get the cumulative total.
5. Print the total miles calculated from the sorted calorie contributions.
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

Implemetation/Code

#include <bits/stdc++.h>
using namespace std;
int main(){
int n;
cin >> n;
int calorie[40];
for(int i = 0; i < n; ++i) cin >> calorie[i];
sort(calorie, calorie + n, greater<int>());
long long total_miles = 0;
for(int i = 0; i < n; ++i) total_miles += (1LL << i) * calorie[i];
cout << total_miles;
return 0;
}

Output
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

Time Complexity : O( N*logn)

Space Complexity : O(n)

Learning Outcomes:-

1. Learn how sorting can impact problem-solving strategies by arranging


elements to maximize or minimize certain conditions
2. Understand the usage of bitwise operations (shifting) in practical
scenarios like exponential multiplication.
3. Develop an intuition for greedy algorithms where arranging input in a
certain order can lead to optimal solutions.
4. Manage basic I/O operations in C++.
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

Experiment 8 B

Student Name: NEMESIS UID: 22BAI


Branch: CSE Section/Group:
Semester: 5 Date of Performance:
Subject Name: DAA Lab Subject Code: 22CSH-311

1. TITLE:
Candies
2. AIM:
Alice is a kindergarten teacher. She wants to give some candies to the children in her
class. All the children sit in a line and each of them has a rating score according to his
or her performance in the class. Alice wants to give at least 1 candy to each child. If
two children sit next to each other, then the one with the higher rating must get more
candies. Alice wants to minimize the total number of candies she must buy.

3. Objective
Return Complete the candies function in the editor below.
candies has the following parameter(s):
int n: the number of children in the class
int arr[n]: the ratings of each student

4. Algorithm
▪ Read the number of elements n, allocate two arrays arr and candy, and input n values
into arr while initializing candy to 1 for each element.
▪ Iterate from the start to the end of the array, incrementing candy count for each
element if it's greater than the preceding element.
▪ Iterate from the end to the start of the array, adjusting the candy count to ensure
every element that's greater than the succeeding one has more candy.
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

▪ Sum all elements in the candy array using accumulate to determine the total number
of candies required.
▪ Print the total number of candies.

6. Implemetation/Code

#include <bits/stdc++.h>
using namespace std;
int main(){
int n;
cin >> n;
int* arr = new int[n];
int* candy = new int[n];
for(int i = 0; i < n; i++) { cin >> arr[i]; candy[i] = 1; }
for(int i = 1; i < n; i++)
if(arr[i] > arr[i-1])
candy[i] = candy[i-1] + 1;
for(int i = n-2; i >= 0; i--)
if(arr[i] > arr[i+1])
candy[i] = max(candy[i], candy[i+1] + 1);
cout << accumulate(candy, candy + n, 0LL);
}

Output
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

6. Time Complexity : O(n)

7. Space Complexity : O(n)

8. Learning Outcomes:-

1. Learn how a greedy approach can be used effectively to solve optimization problems by
making local optimal choices.
2. Gain knowledge on how a two-pass algorithm can solve problems that depend on
conditions from both preceding and succeeding elements.
3. Basic array operations including dynamic memory management in C++.
4. Implement functions from the C++ standard library like accumulate for mathematical
operations on arrays
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

Experiment 8 C

Student Name: NEMESIS UID: 22BAI


Branch: CSE Section/Group:
Semester: 5 Date of Performance:
Subject Name: DAA Lab Subject Code: 22CSH-311

1. TITLE:
Truck Tour
2. AIM:
Suppose there is a circle. There are N petrol pumps on that circle. Petrol pumps are numbered
0 to (N-1) (both inclusive). You have two pieces of information corresponding to each of the
petrol pump: (1) the amount of petrol that particular petrol pump will give, and (2) the distance
from that petrol pump to the next petrol pump. Initially, you have a tank of infinite capacity
carrying no petrol. You can start the tour at any of the petrol pumps. Calculate the first point
from where the truck will be able to complete the circle. Consider that the truck will stop at
each of the petrol pumps. The truck will move one kilometre for each litre of the petrol

3. Objective
The first line will contain the value of N
The next N lines will contain a pair of integers each, i.e. the amount of petrol that petrol pump
will give and the distance between that petrol pump and the next petrol pump.
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

4. Algorithm
o Initialize variables for start index, current balance, and total deficit.
o Iterate over each gas station, calculating balance as difference between petrol and
distance.
o If balance is negative, accumulate deficit, reset balance, and update start to the next
station.
o After iterating through all stations, check if the total balance including the deficit is
non-negative.
o Return the start index if feasible, otherwise return -1 indicating no possible start.

5. Implemetation/Code

#include <bits/stdc++.h>
using namespace std;
int main(){
int N;
cin >> N;
int start = 0, balance = 0, deficit = 0, p, d;
for(int i = 0; i < N; i++){
cin >> p >> d;
balance += p - d;
if(balance < 0){
deficit += balance;
start = i + 1;
balance = 0;
} }
cout << (balance + deficit >= 0 ? start : -1);
}

Output
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

6. Time Complexity : O( n)

7. Space Complexity : O(n)

8. Learning Outcomes:-

o Understanding how to handle input/output efficiently in competitive programming context.


o Learning to apply the greedy algorithm to solve problems related to circular tours or routes.
o Gaining insight into managing balance and deficit to determine feasibility of completing a
loop.
o Practicing conditional logic to update and reset control variables based on runtime
computations.
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

Experiment 8 D

Student Name: NEMESIS UID: 22BAI


Branch: CSE Section/Group:
Semester: 5 Date of Performance:
Subject Name: DAA Lab Subject Code: 22CSH-311

1. TITLE:
Priyanka and Toys

2. AIM:

Priyanka works for an international toy company that ships by container. Her task is
to the determine the lowest cost way to combine her orders for shipping. She has a list
of item weights. The shipping company has a requirement that all items loaded in a
container must weigh less than or equal to 4 units plus the weight of the minimum
weight item. All items meeting that requirement will be shipped in one container.
What is the smallest number of containers that can be contracted to ship the items
based othe given list of weights?

3. Objective

Complete the toys function in the editor below. It should return the minimum number
of containers required to ship toys has the following parameter(s):
w: an array of integers that represent the weights of each order to ship

4. Algorithm
• Read the number of elements N and exit early if N is 0, outputting 0.
• Input the weights into a static array of fixed maximum size and sort it.
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

• Initialize counter for number of groups and set first weight as starting
reference.
• Iterate through the sorted weights, forming new groups when a weight
exceeds the current reference by more than 4 units.
• Output the total number of groups formed.

5. Implemetation/Code

#include <bits/stdc++.h>
using namespace std;
int main() {
int N;
cin >> N;
if (N == 0) return cout << 0, 0;
int weights[100000];
for (int i = 0; i < N; i++) cin >> weights[i];
sort(weights, weights + N);
int count = 1, minimum = weights[0];
for (int i = 1; i < N; i++)
if (weights[i] > minimum + 4) {
count++;
minimum = weights[i];
}
cout << count;
}
Output
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

6. Time Complexity : O( N*LogN)

7. Space Complexity : O(1)

8. Learning Outcomes:-

1. Understand how to manage standard input and output efficiently in


competitive programming.
2. Learn to use arrays for handling fixed-size data and the sorting
function to prepare data for logic processing.
3. Grasp the concept of greedy algorithms by incrementally building a
solution and updating a reference point.
4. Develop an understanding of conditionally manipulating counters
based on comparisons during array iteration.
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

Experiment 8 E

Student Name: NEMESIS UID: 22BAI


Branch: CSE Section/Group:
Semester: 5 Date of Performance:
Subject Name: DAA Lab Subject Code: 22CSH-311

1. TITLE:
Luck Balance

2. AIM:

Lena is preparing for an important coding competition that is preceded by a number of


sequential preliminary contests. Initially, her luck balance is 0. She believes in "saving
luck," and wants to check her theory. Each contest is described by two integers, L[i]
and T[i]:
• L[i] is the amount of luck associated with a contest. If Lena wins the contest, her luck
balance will decrease by L[i]; if she loses it, her luck balance will increase by L[i].
• T[i] denotes the contest's importance rating. It's equal to 1 if the contest is
important, and it's equal to 0 if it's unimportant.

3. Objective

Complete the toys function in the editor below. It should return the minimum number
of containers required to ship toys has the following parameter(s):
w: an array of integers that represent the weights of each order to ship

Algorithm

o Read the number of items N and the number of important items to maximize K.
o For each of the N items, add its value to luck if it's unimportant (T == 0), or store it in the
important array if it's important (T == 1).
DEPARTMENT OF

o Sort the important array in descending order to prioritize higher values.


o Iterate through the sorted important array, adding the value to luck if within the top K
important items, otherwise subtracting it.
o Display the final calculated luck value.

Implemetation/Code

#include <bits/stdc++.h>
using namespace std;
int main(){
int N, K, luck=0, cnt=0;
cin >> N >> K;
int important[100000];
for(int i=0;i<N;i++){
int L, T; cin >> L >> T;
if(T) important[cnt++] = L;
else luck += L;
}
sort(important, important + cnt, greater<int>());
for(int i=0;i<cnt;i++) luck += (i < K ? important[i] : -important[i]);
cout << luck;
}

OUTPUT
DEPARTMENT OF

Time Complexity : O( N*LogN)

Space Complexity : O(N)

Learning Outcomes:-

o Learn to manage and manipulate fixed-size arrays for data storage


o Apply the sort function with a custom comparator (greater<int>()) to arrange data in
descending order.
o Practice implementing conditional checks within loops to perform different operations
o Practicing the implementation of problem-solving strategies in C++.

You might also like