Name: Nishith Dubey
Enrollment No: 0801CS231087
Section: B
Start Date: 11/08/2025
End Date: 15/08/2025
Assignment 4 – Solutions
Question 1. Minimum Possible Loss
Alice must buy a house in one year and sell it later, but at a loss.
Find the minimum loss possible based on given yearly prices.
Solution:
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
long[] prices = new long[n];
for (int i = 0; i < n; i++) prices[i] = sc.nextLong();
class PriceYear {
long price;
int year;
PriceYear(long p, int y) { price = p; year = y; }
}
PriceYear[] arr = new PriceYear[n];
for (int i = 0; i < n; i++) arr[i] = new PriceYear(prices[i], i);
Arrays.sort(arr, (a, b) -> Long.compare(b.price, a.price)); // Descending
long minLoss = Long.MAX_VALUE;
for (int i = 0; i < n - 1; i++) {
if (arr[i].year < arr[i + 1].year) { // buy earlier, sell later
minLoss = Math.min(minLoss, arr[i].price - arr[i + 1].price);
}
}
System.out.println(minLoss);
}
}
Question 2. Minimum Cost to Erase Sequence
You have a list of distinct integers.
Two operations: 1. Remove current minimum → cost = x. 2. Remove current maximum → cost =
number of elements to the right of it.
Find the minimum total cost to remove all elements.
Solution:
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt(), x = sc.nextInt();
List<Integer> list = new ArrayList<>();
for (int i = 0; i < n; i++) list.add(sc.nextInt());
int totalCost = 0;
while (!list.isEmpty()) {
int minVal = Collections.min(list);
int maxVal = Collections.max(list);
int minIdx = list.indexOf(minVal);
int maxIdx = list.indexOf(maxVal);
int k = list.size();
int costMin = x;
int costMax = k - maxIdx - 1;
if (costMin <= costMax) {
totalCost += costMin;
list.remove(minIdx);
} else {
totalCost += costMax;
list.remove(maxIdx);
}
}
System.out.println(totalCost);
}
}
O(n2)
Question 3: Palindromic Times
For given start and end times (in 24-hour HHMM format), count the number of times in between that
form a palindrome (same forward and backward).
Solution:
import java.util.*;
public class Main {
static boolean isPalindrome(int h, int m) {
String time = String.format("%02d%02d", h, m);
return time.equals(new StringBuilder(time).reverse().toString());
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int T = sc.nextInt();
while (T-- > 0) {
String s = sc.next(), e = sc.next();
int sh = Integer.parseInt(s.substring(0, 2));
int sm = Integer.parseInt(s.substring(2));
int eh = Integer.parseInt(e.substring(0, 2));
int em = Integer.parseInt(e.substring(2));
int count = 0;
while (true) {
if (isPalindrome(sh, sm)) count++;
if (sh == eh && sm == em) break;
sm++;
if (sm == 60) { sm = 0; sh++; }
}
System.out.println(count);
}
}
}
O(T×1440) = O(T)
Question 4: Last Child in Queue
N children, each has hunger value.
Each turn, a child gets M ladoos: o If hunger ≤ M → goes home. o If hunger > M → hunger reduces
by M and child goes to end of queue.
Find the index of the last child remaining.
Solution:
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
while (t-- > 0) {
int n = sc.nextInt(), M = sc.nextInt();
int lastChild = -1, maxRounds = -1;
for (int i = 1; i <= n; i++) {
int hunger = sc.nextInt();
int rounds = (hunger + M - 1) / M; // ceil(hunger / M)
if (rounds >= maxRounds) {
maxRounds = rounds;
lastChild = i; // update last child
}
}
System.out.println(lastChild);
}
}
}
O(n)
Question 5: Bomb Destroying Walls
String of W (wall) and B (bomb).
Each bomb destroys up to 2 walls on both sides.
Find total destroyed walls after all bombs explode.
Solution:
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int T = sc.nextInt();
while (T-- > 0) {
String s = sc.next();
boolean[] destroyed = new boolean[s.length()];
for (int i = 0; i < s.length(); i++) {
if (s.charAt(i) == 'B') {
for (int d = 1; d <= 2; d++) {
if (i - d >= 0 && s.charAt(i - d) == 'W') destroyed[i - d] = true;
if (i + d < s.length() && s.charAt(i + d) == 'W') destroyed[i + d] = true;
}
}
}
int count = 0;
for (int i = 0; i < s.length(); i++)
if (destroyed[i]) count++;
System.out.println(count);
}
}
}
O(T x n)
⋅
(where n = length of string, T = number of test cases).