Shantanu-3 3
Shantanu-3 3
Experiment-3.3
Script / Code:
#include <iostream>
#include <vector>
#include <unordered_map>
using namespace std;
int beautifulPairs(vector<int> A, vector<int> B) {
int n = A.size();
unordered_map<int, int> freqA, freqB;
for (int i = 0; i < n; i++) {
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
freqA[A[i]]++;
freqB[B[i]]++;
}
int common = 0;
for (int i = 0; i < n; i++) {
if (freqA[A[i]] > 0 && freqB[A[i]] > 0) {
common++;
freqA[A[i]]--;
freqB[A[i]]--;
}
}
if (common == n) {
// If all elements are common, change one element to make it non-common
return n - 1;
} else {
return common + 1;
}
}
int main() {
int n;
cin >> n;
vector<int> A(n), B(n);
Output:
Problem 2:
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 cupcakes so far, after
eating a cupcake with calories he must walk at least miles to maintain his weight.
Example
If he eats the cupcakes in the order shown, the miles he will need to walk are . This is not
the minimum, though, so we need to test other orders of consumption. In this case, our
minimum miles is calculated as .
Given the individual calorie counts for each of the cupcakes, determine the minimum
number of miles Marc must walk to maintain his weight. Note that he can eat the
cupcakes in any order.
Function Description
Complete the marcsCakewalk function in the editor below.
marcsCakewalk has the following parameter(s):
• int calorie[n]: the calorie counts for each cupcake
Returns
• long: the minimum miles necessary
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
Script / Code:
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
long long marcsCakewalk(vector<int> calorie) {
// Sort the cupcakes in descending order
sort(calorie.rbegin(), calorie.rend());
long long total_miles = 0;
// Calculate the minimum miles
for (int i = 0; i < calorie.size(); i++) {
total_miles += (1LL << i) * calorie[i];
}
return total_miles;
}
int main() {
int n;
cin >> n;
vector<int> calorie(n);
for (int i = 0; i < n; i++) {
cin >> calorie[i];
}
return 0;
}
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
Output:
LEARNING OUTCOMES :
3. Algorithm Design and Analysis: This experience will enhance their ability
to design and analyze algorithms, helping them apply Branch and Bound
techniques to various real-world scenarios and evaluate their effectiveness.