![]() |
VOOZH | about |
Given two arrays A[] and B[] of length N. Then your task is to choose a range [L, R] in such a way that both the conditions are met, Then the task is to output the Maximum possible sum of such B[L, R].
Examples:
Input: N = 5, A[] = {0, 1, 2, 0, 2}, B[] = {5, 6, 7, 8, 2}
Output: 21
Explanation: If we took the range [L, R] as [2, 4]. It gives A[2, 4] = {1, 2, 0} and B[2, 4] = {6, 7, 8}. The sum of B[2, 4] is 6+7+8 = 21. All the elements of A[2, 4] are unique and B[2, 4] is maximum 21. Both the conditions are satisfied. Therefore, output is 21.Input: N = 3, A[] = {1, 1, 1}, B[] = {3, 4, 5}
Output: 5
Explanation: If we took range [L, R] as [3, 3]. It gives A[3, 3] = {1} and B[3, 3] = {5}. Then sum of B[3, 3] is 5. which is maximum possible according to the given constraints. Therefore, output is 5.
Approach: Implement the idea below to solve the problem
The problem is observation based and can be implemented easily. The problem requires using HashSet Data - Structure.
Steps were taken to solve the problem:
Code to implement the approach:
21
Time Complexity: O(N)
Auxiliary space: O(N), As HashSet is used to store unique elements.