Deustche Bank

Debugging - Minimum Required Value - Java

The code editor gives you code for the following problem statement. However, the solution fails the test cases because of code bugs. Your task is to find and fix all the bugs so that it passes all the test cases.

Problem Statement

You are given two arrays, A and B, of size N consisting of non-negative integers. You have to select K numbers from B and take the bitwise OR of all the array elements in A with the chosen K numbers and add their sum. In other words, take each element from array A and add its bitwise OR with each chosen element from array B to the sum.

Note: You cannot choose two adjacent elements in B.

Task

You have to find the minimum value of K such that the sum is greater than or equal to M. Print -1 if impossible.

Example

Assumptions

Approach

Function Description

Complete the MinReq function provided in the editor. This function takes the following 4 parameters and returns the required answer:

Input Format

Note: This is the input format that you must use to provide custom input (available above the Compile and Test button).

Output Format

Print an integer value that is the minimum K for each of the test cases. Each value that is printed must be line-separated.

Constraints

1 <= T <= 10

1 <= N <= 10^3

0 <= A, B <= 10^9

0 <= M <= 10^18

Code Template

import java.util.Scanner; public class Main { static int MinReq(int N, int[] A, int[] B, long M) { long[][] dp = new long[N + 1][N + 1]; // DP array to store max sums long[] val = new long[N]; // Array to store OR values // Calculate the OR values for each element in A with every element in B for (int j = 0; j < N; j++) { val[j] = 0; // Initialize val[j] for (int i = 0; i < N; i++) { val[j] += (A[i] | B[j]); // Calculate A[i] OR B[j] } } // Initialize DP table for (int i = 0; i <= N; i++) { for (int j = 0; j <= N; j++) { dp[i][j] = Long.MIN_VALUE; // Initialize with minimum value } } dp[0][0] = 0; // Base case: using 0 elements gives a sum of 0 // Fill the DP table for (int i = 1; i <= N; i++) { for (int j = 1; j <= N; j++) { dp[i][j] = dp[i][j - 1]; // Not choosing current B[j] // Choose current B[j], ensure we do not pick adjacent elements if (j >= 2) { dp[i][j] = Math.max(dp[i][j], dp[i - 1][j - 2] + val[j - 1]); // Choosing B[j-1] } } } // Find the minimum K such that the sum is >= M for (int k = 1; k <= N; k++) { for (int j = 0; j <= N; j++) { if (dp[k][j] >= M) { return k; // Minimum K found } } } return -1; // If not possible to reach M } public static void main(String[] args) { Scanner sc = new Scanner(System.in); // Read number of test cases int T = sc.nextInt(); // Loop through each test case for (int t = 0; t < T; t++) { // Read the size of the arrays int N = sc.nextInt(); // Read array A int[] A = new int[N]; for (int i = 0; i < N; i++) { A[i] = sc.nextInt(); } // Read array B int[] B = new int[N]; for (int i = 0; i < N; i++) { B[i] = sc.nextInt(); } // Read the lower limit for the sum M long M = sc.nextLong(); // Call the MinReq function and print the result int result = MinReq(N, A, B, M); System.out.println(result); } sc.close(); } }
#include <iostream> #include <vector> #include <algorithm> #include <climits> using namespace std; int MinReq(int N, vector<int>& A, vector<int>& B, long long M) { vector<vector<long long>> dp(N + 1, vector<long long>(N + 1, LLONG_MIN)); // DP array to store max sums vector<long long> val(N); // Array to store OR values // Calculate the OR values for each element in A with every element in B for (int j = 0; j < N; j++) { val[j] = 0; // Initialize val[j] for (int i = 0; i < N; i++) { val[j] += (A[i] | B[j]); // Calculate A[i] OR B[j] } } dp[0][0] = 0; // Base case: using 0 elements gives a sum of 0 // Fill the DP table for (int i = 1; i <= N; i++) { for (int j = 1; j <= N; j++) { dp[i][j] = dp[i][j - 1]; // Not choosing current B[j-1] // Choose current B[j-1], ensure we do not pick adjacent elements if (j >= 2) { dp[i][j] = max(dp[i][j], dp[i - 1][j - 2] + val[j - 1]); // Choosing B[j-1] } } } // Find the minimum K such that the sum is >= M for (int k = 1; k <= N; k++) { for (int j = 1; j <= N; j++) { if (dp[k][j] >= M) { return k; // Minimum K found } } } return -1; // If not possible to reach M } int main() { int T; cin >> T; // Loop through each test case for (int t = 0; t < T; t++) { // Read the size of the arrays int N; cin >> N; // Read array A vector<int> A(N); for (int i = 0; i < N; i++) { cin >> A[i]; } // Read array B vector<int> B(N); for (int i = 0; i < N; i++) { cin >> B[i]; } // Read the lower limit for the sum M long long M; cin >> M; // Call the MinReq function and print the result int result = MinReq(N, A, B, M); cout << result << endl; } return 0; }

Problem: Calculate Sum

Problem Description

A function S(x) is defined as the sum of all divisors of x. For example:

Now, let us define a function F(x) as the sum of S(y) where y is a divisor of x, i.e., y divides x.

You are given multiple tasks. In each task, you are provided two integers L and R. Your task is to calculate the sum of F(x) for all x from L to R, i.e., compute the sum F(L) + F(L+1) + ... + F(R).

Input Format

Output Format

For each task, print a single integer in a new line representing the sum of F(x) for all x in the range [L, R].

Constraints

Example Input

Example Output

Explanation

For the given example where L = 8 and R = 10:

Thus, F(8) + F(9) + F(10) = 26 + 18 + 28 = 72.

Code Template

#include <bitset> #include <algorithm> #include <cassert> #include <cctype> #include <cmath> #include <complex> #include <cstdio> #include <cstdlib> #include <cstring> #include <ctime> #include <deque> #include <functional> #include <iomanip> #include <iostream> #include <iterator> #include <limits> #include <list> #include <map> #include <numeric> #include <queue> #include <set> #include <stack> #include <string> #include <unordered_map> #include <unordered_set> #include <utility> #include <vector> #include <fstream> #include <sstream> using namespace std; vector<int> S(1000001, 0); vector<int> F(1000001, 0); vector<long long> prefix_sum(1000001, 0); signed main() { for (int i = 1; i <= 1e6; i++) { for (int j = i; j <= 1e6; j += i) { S[j] += i; } } for (int i = 1; i <= 1e6; i++) { for (int j = i; j <= 1e6; j += i) { F[j] += S[i]; } } for (int i = 1; i <= 1e6; i++) { prefix_sum[i] = prefix_sum[i - 1] + F[i]; } int n; cin >> n; while (n--) { int a, b; cin >> a >> b; cout << prefix_sum[b] - prefix_sum[a - 1] << endl; } return 0; }

Problem: Value of an Expression

Problem Description

Given a sequence of n integers a1, a2, ..., an, you are asked to perform the following operation and return the obtained result:

For a given integer x, calculate the value of the expression min(m₁, m₂, ..., mz-1) where:

In other words, you need to find the minimum of the values m₁, m₂, ..., m_{z-1}.

Function Description

Complete the solve function provided in the editor. The function takes the following 3 parameters and returns the answer:

Input Format

The first line contains two integers x and n.

The second line contains n space-separated integers representing the array a.

Output Format

Print an integer denoting the value of the expression.

Constraints

Example Input

Example Output

Explanation

Thus, min(m₁, m₂, m₃) = 6.

Notes

Your code must print the correct output for the provided sample input. However, it will be tested against hidden test cases, so make sure your solution is optimized and correct.

Problem: Black and White Tree

Problem Description

Given a tree with N vertices labeled from 1 to N, rooted at vertex 1. The tree is an undirected graph with N-1 edges. Each edge i connects two vertices a_i and b_i. Each vertex i is colored either white (represented by '0') or black (represented by '1').

The beauty of a vertex i is defined as the number of paths in its subtree that have end vertices of the opposite color.

You are required to find the beauty of all N vertices.

Definitions

Function Description

Complete the function solve. This function takes the following 3 parameters:

The function should return the beauty for all N vertices as space-separated integers.

Input Format for Custom Testing

  1. The first line contains a single integer N, denoting the number of vertices in the tree.
  2. The second line contains a string Color of size N, consisting of characters '0' (white) and '1' (black).
  3. The next N-1 lines contain two integers a_i and b_i, denoting an undirected edge between the vertices numbered a_i and b_i.

Output Format

Print N space-separated integers, where each integer denotes the beauty of the corresponding vertex.

Constraints

Example Input

5 01101 3 1 4 3 5 3 2 4

Example Output

4 0 3 0 0

Explanation

For the given tree:

Thus, the answer is 4 0 3 0 0.

Notes

Limits

Problem: The Maze Runner

Problem Description

You bought two virtual reality glasses. Each glasses has a unique feature:

  1. The first glasses allows you to pass through doors.
  2. The second glasses allows you to walk through walls.

The maze is represented as a grid of size N x M. You start at the top-left corner of the grid and your goal is to reach the bottom-right corner. You can move vertically or horizontally, and each movement takes 1 minute.

Some cells in the maze contain obstacles:

You need to determine how long it takes to reach the bottom-right corner of the maze using each pair of glasses. If it is impossible to reach the goal with either pair, return -1 for that pair.

Function Description

Complete the function solve. This function takes the following 3 parameters and returns an array of length 2:

The function should return an array with two integers:

If it's impossible to reach the bottom-right corner using either glasses, return -1 for that glasses.

Input Format

Output Format

If reaching the bottom-right corner is not possible with either glasses, print -1.

Constraints

Example Input

5 5 ..#.. .#.#. ..#.. #+.#. …..

Example Output

-1 8

Explanation

Notes

Time Limit

Problem: Find the Minimum Sum After Operations

Problem Description

You are given an array of integers of size N. You need to find the minimum possible sum that can be formed by adding all the elements in the array after performing the following operation at most once:

  1. Operation: Select an index i. If the sum of all the elements from index 0 to index i-1 is divisible by the element at index i, divide the sum by the element at index i.
  2. After dividing, you do not have to add the element at index i to the resultant sum. Instead, you will add all other elements from index i+1 to the end of the array.

Return the minimum possible sum that can be obtained after performing the operation once.

Notes

Function Description

Complete the function solve. This function takes the following 2 parameters and returns a 64-bit integer representing the minimum possible sum:

Input Format

Output Format

For each test case, output a single integer representing the minimum possible sum after performing the operation.

Constraints

Sample Input

2 5 1 4 5 5 10 3 100 2 1

Sample Output

12 3

Explanation

Input

The first line contains T, denoting the number of test cases.

For the first test case:

Given:

Approach

We evaluate the possible operations for each index:

Description:
Given an integer array nums of size N and an integer K, find the largest positive number less than or equal to (10^9) such that at least K numbers from the array nums are divisible by it.

Function Signature

Input Format

-	The first line contains an integer `T`, representing the number of test cases.
-	For each test case:
-	The first line contains an integer `N`, denoting the size of array `nums`.
-	The second line contains integer `K`.
-	The third line contains `N` spaced integers of the array `nums`.

Output Format

-	For each test case, print the required answer on a new line.

Constraints

1<= T <= 1000 1<= N <= 10^5 1<= K <= N 1<= nums[i] <= 10^6, where 1<= i <= N

Sample Input

2 5 3 12 16 32 2 45 5 3 12 16 32 2 0

Sample Output

4 16

Explanation

-	In the first test case, the largest positive integer that divides at least 3 numbers in the array is 4.
-	In the second test case, the largest positive integer that divides at least 3 numbers in the array is 16.