![]() |
VOOZH | about |
Given two arrays X[] and Y[] of size N, the task is to find the longest subarray in X[] containing only unique values such that a subarray with similar indices in Y[] should have a maximum sum. The value of array elements is in the range [0, 1000].
Examples:
Input: N = 5,
X[] = {0, 1, 2, 0, 2},
Y[] = {5, 6, 7, 8, 22}
Output: 21
Explanation: The largest unique subarray in X[] with maximum sum in Y[] is {1, 2, 0}.
So, the subarray with same indices in Y[] is {6, 7, 8}.
Therefore maximum sum is 21.Input: N = 3,
X[] = {1, 1, 1},
Y[] = {2, 6, 7}
Output: 7
Naive Approach: The task can be solved by generating all the subarrays of the array X[], checking for each subarray if it is valid, and then calculating the sum in the array for corresponding indices in Y.
Time Complexity: O(N3)
Auxiliary Space: O(N)
Efficient Approach: The task can be solved using the concept of the sliding window. Follow the below steps to solve the problem:
Below is the implementation of the above approach:
30
Time Complexity: O(N)
Auxiliary Space: O(N)