GOOGLE Coding Question – Solved

12 Live
Remaining elements You are given two arrays A and B of size N. Your task is to remove some numbers from array A such that the following conditions are met: 1. The remaining elements of array A are strictly increasing from left to right. 2. The corresponding elements from B must also be extracted. For example, if you have removed the ith element of array A, then the ith element of array B must also be pulled out. Consider 1-based indexing. Find the maximum strength of the remaining elements of array B. Suppose the remaining elements of array B are Bn, Br2, ..., Bir, 1 ≤ I1, I2, ..., Ir < N, then the strength of array B is calculated as B1 - Bm + Bg - .... (-1)^r-1 * B. Note: 1-based indexing is followed. Input format: - The first line contains an integer T denoting the number of test cases. - The next line contains an integer N denoting the size. - The next two lines contain N space-separated integers denoting the elements of arrays A and B respectively. Output format: - Print the maximum strength of the remaining elements of array B in a new line. Constraints: - 1 ≤ T ≤ 10 - 1 ≤ N ≤ 2 × 10^3 - 1 ≤ Ai, Bi ≤ 10^6 Sample input: 7 3 1 4 5 6 20 30 40 10 20 Sample output: 50 85

Asked in: GOOGLE

Image of the Question

Question Image Question Image Question Image Question Image Question Image

All Testcases Passed ✔



Passcode Image

Solution


#include <bits/stdc++.h>
using namespace std;

const int INF = 1e9;
// ... rest of solution available after purchase

🔒 Please login to view the solution

Explanation


```
To approach this problem, start by understanding the key conditions and how they interact. You are given two arrays, A and B, and your goal is to select a strictly increasing subsequence from A, removing elements as needed, while also preserving the relative positions of elements in B that correspond to the selected elements in A. The constraint is that you can only pick elements in order (i.e., their original sequence in A must be maintained), and each selection from A forces you to select the corresponding value from B.

The central challenge is to find such a strictly increasing subsequence from A (not necessarily contiguous), and then compute the "strength" of the corresponding elements from B, using an alternating sum formula. Specifically, if you select r elements from B, the strength is computed as B1 - B2 + B3 - B4 + ... + (-1)^(r-1) * Br. The task is to maximize this strength value.

The first thing to realize is that this problem is an extension of the classic Longest Increasing Subsequence (LIS) problem, but instead of just maximizing the length, you must optimize a custom function (alternating sum from B) based on the valid increasing subsequences from A. This adds a layer of dynamic programming to the problem, where you need to track not just subsequence lengths or values, but also the best possible strength at each position, depending on how many elements have been selected so far.

Begin by thinking in terms of dynamic programming states. You need to decide, at each index in A, whether to include that element as part of your increasing subsequence or skip it. If you include it, you must ensure that the element maintains the increasing property with respect to the last selected element. You also need to keep track of how many elements you've selected so far, because the sign of the corresponding B element (positive or negative) depends on its position in the selection (odd or even index in the subsequence).

Therefore, each DP state can be defined by two things:
1. The current index in A being considered.
2. The number of elements selected so far (which affects the sign in the strength calculation).
3. The index or value of the last selected element from A (to ensure strict increase).

You will iterate through A, and at each step, evaluate the possibility of extending a subsequence by including the current element or skipping it. If you include it, you must:
- Check that it is greater than the last included A element.
- Determine the correct sign for the corresponding B value, based on whether it's the 1st, 2nd, 3rd, etc., element in the subsequence.
- Add or subtract the B value accordingly to compute the running strength.

You must also consider memoization or tabulation to avoid recomputing overlapping subproblems. A 2D or 3D dynamic programming table could be used, where each cell stores the best strength obtainable up to that point with a certain number of selected elements and a specific last element index.

As the array size N can go up to 2000, an O(N^2) or O(N^3) solution may be acceptable with optimizations, but you should avoid recomputing states needlessly. Efficient state transitions and pruning unnecessary checks (like avoiding decreasing selections) will help reduce the computational overhead.

Finally, after processing the entire array, your result will be the maximum strength obtainable across all possible valid subsequences. This means scanning your DP table for the best result achieved at any final index and any valid count of selected elements.

In summary, follow these steps:
1. Iterate over the array while building up strictly increasing subsequences.
2. For each valid subsequence extension, compute the strength using the alternating sum pattern.
3. Use dynamic programming to track the maximum strength for each possible selection count and endpoint.
4. At the end, find the highest value among all final DP states.

By thinking carefully about state transitions and the impact of element selection order on the strength formula, you can navigate the trade-off between sequence length and strength value to reach the optimal solution.
```


Related Questions