0% found this document useful (0 votes)
51 views8 pages

Assignment 4

The document contains multiple programming assignments involving algorithm design and analysis. Each assignment presents a problem statement, input/output formats, and sample code in Java to solve the problem. Topics include calculating minimum loss from house prices, minimizing costs to erase a sequence of integers, counting palindromic times, managing children's hunger in a queue, and determining the number of walls destroyed by bombs.

Uploaded by

pjbrowtf
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)
51 views8 pages

Assignment 4

The document contains multiple programming assignments involving algorithm design and analysis. Each assignment presents a problem statement, input/output formats, and sample code in Java to solve the problem. Topics include calculating minimum loss from house prices, minimizing costs to erase a sequence of integers, counting palindromic times, managing children's hunger in a queue, and determining the number of walls destroyed by bombs.

Uploaded by

pjbrowtf
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

Assignment - 4

Design and Analysis of Algorithms


[Link] has a list showing the projected values of a house for the next several years. She must
buy the house in one year and sell it in a later year, but the sale must result in a loss. Her goal is
to minimize that loss. For example, if the projected prices over the next n=5 years are:
prices=[20,15,8,2,12]. The smallest loss occurs if she buys in year 2 at 15 and sells in year 5 at
12, losing only 3. Given the list of projected prices, determine and print the minimum possible
loss Alice can incur if she buys a house and resells it in some later year. Input Format The first
line contains an integer n , the number of years of house data. The second line contains n
space-separated long integers describing each price[i]. Constraints ● 2 <= n <= 2*10^5 ● 1 <=
price[i] <= 10^16 ● All the prices are distinct. Output Format Print a single integer denoting the
minimum amount of money Alice must lose if she buys and resells the house within the next n
years. Sample Input 3 5 10 3 Sample Output 2 Sample Input 5 20 7 8 2 5 Sample Output 2

Code:
import [Link].*;

class LossCalculator {
private long[] prices;

public LossCalculator(long[] prices) {


[Link] = prices;
}

public long getMinimumLoss() {


TreeSet<Long> seen = new TreeSet<>();
long minLoss = Long.MAX_VALUE;

for (long price : prices) {


Long higher = [Link](price);
if (higher != null) {
minLoss = [Link](minLoss, higher - price);
}
[Link](price);
}
return minLoss;
}
}

public class Main {


public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
int n = [Link]();
long[] prices = new long[n];
for (int i = 0; i < n; i++) {
prices[i] = [Link]();
}

LossCalculator calculator = new LossCalculator(prices);


[Link]([Link]());
[Link]();
}
}

Output:

[Link] are given a sequence of distinct integers a1,a2,....an and an integer x and you have to
perform the following operations: 1. Remove the current minimum from a. This operation costs x
. 2. Remove the current maximum from a . This operation costs the number of elements located
on the right to the maximum. Formally, if the maximum is ai and the current size of the sequence
is k , then the cost will be k-i - What is the minimum cost to erase the whole sequence? Input
format ● First line: Two integers n and x, denoting the size of the sequence and the cost of the
first type of operation ● Second line: A sequence a1,a2,....an (1<=ai<=109) ● It is guaranteed
that all elements of a are distinct. Output format Print an integer denoting the minimum cost to
erase the whole sequence.

Output:
import [Link].*;
class Element{int value,index;Element(int value,int index){[Link]=value;[Link]=index;}}
class EraseCostCalculator{
private int n,x;private int[] a;
public EraseCostCalculator(int n,int x,int[] a){this.n=n;this.x=x;this.a=a;}
public long getMinimumCost(){
TreeMap<Integer,Integer> map=new TreeMap<>();
for(int i=0;i<n;i++) [Link](a[i],i);
boolean[] removed=new boolean[n];
long totalCost=0;int remaining=n;
while(remaining>0){
int
minVal=[Link](),maxVal=[Link](),minIdx=[Link](minVal),maxIdx=[Link](maxVal
);
long costMin=x,costMax=0;
for(int i=maxIdx+1;i<n;i++) if(!removed[i]) costMax++;
if(costMin<=costMax){totalCost+=costMin;removed[minIdx]=true;[Link](minVal);}
else{totalCost+=costMax;removed[maxIdx]=true;[Link](maxVal);}
remaining--;
}
return totalCost;
}
}
public class Main{
public static void main(String[] args){
Scanner sc=new Scanner([Link]);
int n=[Link](),x=[Link]();
int[] a=new int[n];
for(int i=0;i<n;i++) a[i]=[Link]();
EraseCostCalculator calc=new EraseCostCalculator(n,x,a);
[Link]([Link]());
[Link]();
}
}

Output:
[Link] programmer friend Bob just taught his son, Bob Junior, how to read a clock. A few days
back he also taught him about palindromes. Now as we know that Micro Junior looks for
opportunities to trouble his father, so he started asking him questions. He asked him T
questions. In each he gave his father a starting time and an ending time and asked the number
of times in between which are palindromes (see sample explanation for more clarity). Now Micro
asked for your help to solve this problem. Input: The first line consists of T, the number of
questions. Next T lines consist of a starting time S and an ending time E separated by a space.
Time is given in 24-hour format without any colons. (See sample input) Output: Print the answer
for each question in a new line. Constraints: 1 ≤ T ≤ 1000 It is assured that every time given is
valid. Time starts from 0000 to 2359 S ≤ E Sample Input 3 0100 0200 1100 1300 1331 1441
Sample Output 1 2 2

Code:
import [Link].*;
public class Main {
public static boolean isPalindrome(int time) {
String s = [Link]("%04d", time);
return [Link](0) == [Link](3) && [Link](1) == [Link](2);
}
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
int T = [Link]();
int[] results = new int[T];
for (int t = 0; t < T; t++) {
int S = [Link]();
int E = [Link]();
int count = 0;
for (int time = S; time <= E; time++) {
int hours = time / 100;
int minutes = time % 100;
if (hours >= 0 && hours <= 23 && minutes >= 0 && minutes <= 59) {
if (isPalindrome(time)) count++;
}
}
results[t] = count;
}
[Link]();
for (int res : results) [Link](res);
}
}
Output:

[Link] is very thrilled to have won unlimited ladoos in a programming competition. But
being a generous programmer he decided to give away all his ladoos to small children. Now
there are n children standing in a queue where a[i] denotes the hunger of ith child. Codehunt
can give a maximum of M ladoos to a child at a time. Now there are 2 conditions : ● If a child
gets equal to or more ladoos than his hunger , then he will return back to his home. ● If a child
gets less ladoos , he will take m ladoos and will move to last in the queue to get his turn again.
Constraints : 1<=n,m<=10000 1<=a[i]<=10000000 Input: The first line contains integer t
denoting the test cases , next t line follows : Now you will be given N,M . Next line will contain n
space separated integers . Output: Output the index of the child who will remain last in the
queue. Sample Input 2 5 2 1 3 1 4 2 6 4 1 1 2 2 3 3 Sample Output 4 6

Code:
import [Link].*;

class Child {
int index;
int hunger;
Child(int index, int hunger) {
[Link] = index;
[Link] = hunger;
}
}

public class Main {


public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
int t = [Link]();
int[] results = new int[t];
for (int test = 0; test < t; test++) {
int n = [Link]();
int M = [Link]();
Queue<Child> queue = new LinkedList<>();
for (int i = 1; i <= n; i++) {
[Link](new Child(i, [Link]()));
}
int lastIndex = 0;
while (![Link]()) {
Child c = [Link]();
if ([Link] > M) {
[Link] -= M;
[Link](c);
} else {
lastIndex = [Link];
}
}
results[test] = lastIndex;
}
[Link]();
for (int res : results) [Link](res);
}
}

Output:
[Link] and Khatu are brave soldiers in World War 3. They have spotted an enemy troop which is
planting bombs. They sent a message to the command centre containing characters W and B
where W represents a wall and B represents a Bomb. They asked command to tell them how
many walls will be destroyed if all bombs explode at once. One bomb can destroy 2 walls on
both sides. Input: First line of input contains number of test cases T. Each test case contains a
single string which contains two type of chars 'W' and 'B'. Output: For each test case print the
total number of destroyed wall. Constraints: 1 ≤ T ≤ 10 1 ≤ |S| ≤ 105 Sample Input 3 WBW
WWBWWBW BWWWBWBWW Sample Output 2 5 6

Code:
import [Link].*;

public class Main {


public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
int T = [Link]();
[Link]();
int[] results = new int[T];

for (int t = 0; t < T; t++) {


String s = [Link]();
int n = [Link]();
boolean[] destroyed = new boolean[n];

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


if ([Link](i) == 'B') {
for (int j = i - 2; j <= i + 2; j++) {
if (j >= 0 && j < n && [Link](j) == 'W') destroyed[j] = true;
}
}
}

int count = 0;
for (boolean b : destroyed) if (b) count++;
results[t] = count;
}

for (int res : results) [Link](res);


}
}
Output:

You might also like