import java.util.*;class Main { public static long solve(int N, int lower, int upper, int[] a) {// Compute prefix sums of volume changes long[] p = new long[N + 1]; p[0] = 0; for (int i = 0; i < N; i++) { p[i + 1] = p[i] + a[i]; }// 1. Calculate prefix comfortable count (without any reset) int[] prefixComfort = new int[N + 1]; long currentFloor = 0; for (int i = 1; i <= N; i++) { prefixComfort[i] = prefixComfort[i - 1]; long diff = p[i] - currentFloor; if (diff >= lower && diff <= upper) { prefixComfort[i]++; } currentFloor = Math.min(currentFloor, p[i]); }// Default max comfortable moments without pressing reset long maxComfortable = prefixComfort[N];// 2. Try resetting floor after each event k (0 <= k < N)// k = 0 means reset before event 1 for (int k = 0; k < N; k++) { int comfortableAfterReset = 0; long floorAfterReset = p[k]; // Reset sets floor baseline to p[k] for (int j = k + 1; j <= N; j++) { long diff = p[j] - floorAfterReset; if (diff >= lower && diff <= upper) { comfortableAfterReset++; } floorAfterReset = Math.min(floorAfterReset, p[j]); } long totalComfortable = (long) prefixComfort[k] + comfortableAfterReset; maxComfortable = Math.max(maxComfortable, totalComfortable); } return maxComfortable; } public static void main(String[] args) { Scanner sc = new Scanner(System.in); if (!sc.hasNextInt()) return; int N = sc.nextInt(); int lower = sc.nextInt(); int upper = sc.nextInt(); int[] a = new int[N]; for (int i = 0; i < N; i++) { a[i] = sc.nextInt(); } long result = solve(N, lower, upper, a); System.out.println(result); }}
Question 2 :- Quantum Core Parity
Solution in Java
importjava.util.*;classMain {privatestaticfinalintMOD = 1000000007;publicstaticintsolve(intN) {if (N <= 0) return 0;// dp[last_val][parity]// last_val: 0, 1, 2// parity: 0 = EVEN, 1 = ODDlong[][] dp = newlong[3][2];// Base case for N = 1// Node 0: value 0 -> Even parity (0)dp[0][0] = 1;// Node 1: value 1 -> Odd parity (1)dp[1][1] = 1;// Node 2: value 2 -> Even parity (0)dp[2][0] = 1;for (inti = 2; i <= N; i++) {long[][] nextDp = newlong[3][2];// Append 0 (adds 0 energy -> parity doesn't change)// Cannot place after 0nextDp[0][0] = (dp[1][0] + dp[2][0]) % MOD;nextDp[0][1] = (dp[1][1] + dp[2][1]) % MOD;// Append 1 (adds 1 energy -> flips parity)// Can place after 0, 1, 2longsumEven = (dp[0][0] + dp[1][0] + dp[2][0]) % MOD;longsumOdd = (dp[0][1] + dp[1][1] + dp[2][1]) % MOD;nextDp[1][0] = sumOdd; // odd parity becomes evennextDp[1][1] = sumEven; // even parity becomes odd// Append 2 (adds 2 energy -> parity doesn't change)// Can place after 0, 1, 2nextDp[2][0] = sumEven;nextDp[2][1] = sumOdd;dp = nextDp; }// Sum configurations of length N with EVEN parity (0)longans = (dp[0][0] + dp[1][0] + dp[2][0]) % MOD;return (int) ans; }publicstaticvoidmain(String[] args) {Scannersc = newScanner(System.in);if (!sc.hasNextInt()) return;intN = sc.nextInt();intresult = solve(N);System.out.println(result); }}
Question 3 :- Longest Increasing Path in a Matrix
Solution in Java
importjava.util.*;classMain {publicstaticintsolve(intm, intn, int[][] matrix) {if (m == 0 || n == 0) return 0;// Group cells by their matrix valuesTreeMap<Integer, List<int[]>> valueMap = newTreeMap<>();for (intr = 0; r < m; r++) {for (intc = 0; c < n; c++) {valueMap.computeIfAbsent(matrix[r][c], k -> newArrayList<>()).add(newint[]{r, c}); } }int[][] dp0 = newint[m][n];int[][] dp1 = newint[m][n];int[] dr = {-1, 1, 0, 0};int[] dc = {0, 0, -1, 1};intmaxDp0Larger = 0; // Maximum dp0 value across all strictly larger matrix elementsintoverallMax = 0;// Process values in reverse (from largest matrix value down to smallest)List<Integer> sortedValues = newArrayList<>(valueMap.keySet());for (inti = sortedValues.size() - 1; i >= 0; i--) {intval = sortedValues.get(i);List<int[]> cells = valueMap.get(val);// Compute dp values for all cells having the current matrix valuefor (int[] cell : cells) {intr = cell[0];intc = cell[1];intmaxDp0FromAdj = 0;intmaxDp1FromAdj = 0;for (intd = 0; d < 4; d++) {intnr = r + dr[d];intnc = c + dc[d];if (nr >= 0 && nr < m && nc >= 0 && nc < n && matrix[nr][nc] > val) {maxDp0FromAdj = Math.max(maxDp0FromAdj, dp0[nr][nc]);maxDp1FromAdj = Math.max(maxDp1FromAdj, dp1[nr][nc]); } }dp0[r][c] = 1 + maxDp0FromAdj;// Can move to adjacent with dp1 OR teleport to any strictly larger cell with dp0dp1[r][c] = 1 + Math.max(maxDp1FromAdj, maxDp0Larger);overallMax = Math.max(overallMax, Math.max(dp0[r][c], dp1[r][c])); }// Update maxDp0Larger with the newly calculated dp0 values for this value groupfor (int[] cell : cells) {intr = cell[0];intc = cell[1];maxDp0Larger = Math.max(maxDp0Larger, dp0[r][c]); } }returnoverallMax; }publicstaticvoidmain(String[] args) {Scannersc = newScanner(System.in);if (!sc.hasNextInt()) return;intm = sc.nextInt();intn = sc.nextInt();int[][] matrix = newint[m][n];for (inti = 0; i < m; i++) {for (intj = 0; j < n; j++) {matrix[i][j] = sc.nextInt(); } }intresult = solve(m, n, matrix);System.out.println(result); }}