0% found this document useful (0 votes)
22 views5 pages

AP 10 Ankit

Ankit Chaurasiya completed Experiment 10 for the subject Advance Programming - 1. The experiment involved two coding problems - one using a greedy algorithm to calculate the maximum calories Marc can burn from eating cupcakes, and another using a branch and bound approach to count the number of beautiful pairs between two arrays. The code was written and tested with the provided inputs and outputs. The learning outcomes included fundamentals of branch and bound algorithms, algorithmic thinking, and debugging and optimization techniques.

Uploaded by

Ankit Chaurasiya
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views5 pages

AP 10 Ankit

Ankit Chaurasiya completed Experiment 10 for the subject Advance Programming - 1. The experiment involved two coding problems - one using a greedy algorithm to calculate the maximum calories Marc can burn from eating cupcakes, and another using a branch and bound approach to count the number of beautiful pairs between two arrays. The code was written and tested with the provided inputs and outputs. The learning outcomes included fundamentals of branch and bound algorithms, algorithmic thinking, and debugging and optimization techniques.

Uploaded by

Ankit Chaurasiya
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

DEPARTMENT OF

COMPUTER SCIENCE & ENGINEERING

Experiment 10
Student Name:Ankit Chaurasiya UID:21BCS11172
Branch:BE-CSE Section/Group: 604-A
Semester:5th Date : 31-10-23
Subject Name:Advance Programming - 1 Subject Code: 21CSP-314

Aim: Greedy and Branch and Bound

Objective: (i) 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.

Code :

import java.io.*;
import java.math.*;
import java.security.*;
import java.text.*;
import java.util.*;
import java.util.concurrent.*;
import java.util.function.*;
import java.util.regex.*;
import java.util.stream.*;
import static java.util.stream.Collectors.joining;
import static java.util.stream.Collectors.toList;

class Result {

/*
* Complete the 'marcsCakewalk' function below.
*
* The function is expected to return a LONG_INTEGER.
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

* The function accepts INTEGER_ARRAY calorie as parameter.


*/

public static long marcsCakewalk(List<Integer> calorie) {


// Write your code here
long total = 0;
int j = 0;
Collections.sort(calorie);
for (int i = calorie.size() - 1; i >= 0; i--) {
total += (long) calorie.get(i) * (long) Math.pow(2, j);
j++;
}
return total;
}

public class Solution {


public static void main(String[] args) throws IOException {
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in)
);
BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(System.getenv("OUT
PUT_PATH")));

int n = Integer.parseInt(bufferedReader.readLine().trim());

List<Integer> calorie = Stream.of(bufferedReader.readLine().replaceAll("\\s+$", "").split("


"))
.map(Integer::parseInt)
.collect(toList());

long result = Result.marcsCakewalk(calorie);

bufferedWriter.write(String.valueOf(result));
bufferedWriter.newLine();

bufferedReader.close();
bufferedWriter.close();
}
}
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

1. Output:

(ii) You are given two arrays, and , both containing integers. A pair of indices is beautiful if

the element of array is equal to the element of array . In other words, pair is beautiful if and

only if . A set containing beautiful pairs is called a beautiful set.

Code:

import java.io.*;
import java.math.*;
import java.security.*;
import java.text.*;
import java.util.*;
import java.util.concurrent.*;
import java.util.function.*;
import java.util.regex.*;
import java.util.stream.*;
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

import static java.util.stream.Collectors.joining;


import static java.util.stream.Collectors.toList;

class Result {
public static int beautifulPairs(List<Integer> A, List<Integer> B) {
int countBeautifulPairs = 0;
for(int i = 0 ; i < A.size(); i++){
if(B.contains(A.get(i))){countBeautifulPairs++;
B.remove(B.indexOf(A.get(i)));
}
}
countBeautifulPairs+= (countBeautifulPairs==A.size()) ? -1 : 1;
return countBeautifulPairs;
}
}

public class Solution {


public static void main(String[] args) throws IOException {
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in)
);
BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(System.getenv("OUT
PUT_PATH")));

int n = Integer.parseInt(bufferedReader.readLine().trim());

List<Integer> A = Stream.of(bufferedReader.readLine().replaceAll("\\s+$", "").split(" "))


.map(Integer::parseInt)
.collect(toList());

List<Integer> B = Stream.of(bufferedReader.readLine().replaceAll("\\s+$", "").split(" "))


.map(Integer::parseInt)
.collect(toList());

int result = Result.beautifulPairs(A, B);

bufferedWriter.write(String.valueOf(result));
bufferedWriter.newLine();

bufferedReader.close();
bufferedWriter.close();
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

}
}

2. Output:

Learning Outcome:

Basics of Branch and Bound


Algorithmic Thinking
Debugging and Optimization

You might also like