AP LAB -8TH merged using functions
AP LAB -8TH merged using functions
Experiment 8 A
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
Learning Outcomes:-
Experiment 8 B
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
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
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)
8. Learning Outcomes:-
Experiment 8 D
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
8. Learning Outcomes:-
Experiment 8 E
1. TITLE:
Luck Balance
2. AIM:
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
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
Learning Outcomes:-