![]() |
VOOZH | about |
Given two integers N, K, and two arrays A and B each of size N, such that for every index i, A[i] lies between [1, 104] and B[i] lies between [1, 109]. Find the maximum subarray length [l, r] such that:
Examples:
Input: N = 5, A[] = {3, 2, 4, 1, 8}, B[] = {4, 4, 2, 4, 1}, K = 12
Output: 3
Explanation: A valid and max length subarray can be l = 0, r = 2.Input: N = 4, A[] = {5, 4, 1, 2}, B[] = {6, 2, 3, 1}, K = 8
Output: 2
Explanation: A valid and max length subarray can be l = 2, r = 3.
Approach: To solve the problem follow the below idea:
The problem can be solved by maintaining a sliding window using two pointers. At every index, we will increase the length of the sliding window and check whether the conditions are getting fulfilled on both arrays A and B. If the divisibility check of subarray B fails, then it means that we need to start with a new sliding window from this index. If the sum of subarray A becomes greater than K, we start reducing the size of the window by moving the start pointer forward. After satisfying both the conditions, update the answer.
Steps to solve the problem:
Below is the implementation for the above approach:
3
Time Complexity: O(N), where N is the size of array A[] and B[]
Auxiliary Space: O(1)