Maximum Subarray Length with Divisibility and Sum Constraint
Last Updated :
11 Nov, 2023
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:
- For each index i which lies between [l, r-1], B[i] is divisible by B[i+1] and
- The sum of all the elements in the range [l, r] in array A <= K.
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:
- Maintain 2 pointers, start and end to keep track of the sliding window.
- Initialize the sum of subarray sum = 0.
- Maintain a variable ans to keep track of the length of the longest valid sliding window encountered so far.
- Increment the length of the sliding window by incrementing end by 1.
- Check for the divisibility condition in subarray B:
- If the divisibility check fails, start a new subarray from this index.
- If the divisibility check passes, check if the sum of subarray A is less than K.
- If sum <= K, update ans if the window’s length is greater than ans.
- If sum > K, decrease the window’s length by moving the start pointer until we have sum <= K and then update ans if the window’s length is greater than ans.
- After iterating over the whole array, return and.
Below is the implementation for the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
long maxLengthSubarray( int n, int a[], int b[], int k)
{
int start = 0, end;
int sum = 0;
int ans = 0;
for (end = 0; end < n; end++) {
if (start == end || b[end - 1] % b[end] == 0) {
sum += a[end];
while (sum > k) {
sum -= a[start++];
}
ans = max(ans, end - start + 1);
}
else {
start = end;
end -= 1;
sum = 0;
}
}
return ans;
}
int main()
{
int n = 5, k = 12;
int a[] = { 3, 2, 4, 1, 8 };
int b[] = { 4, 4, 2, 4, 1 };
cout << maxLengthSubarray(n, a, b, k) << '\n' ;
return 0;
}
|
Java
import java.io.*;
import java.util.*;
public class GFG {
public static long maxLengthSubarray( int n, int [] a,
int [] b, int k)
{
int start = 0 , end;
int sum = 0 ;
int ans = 0 ;
for (end = 0 ; end < n; end++) {
if (start == end || b[end - 1 ] % b[end] == 0 ) {
sum += a[end];
while (sum > k) {
sum -= a[start++];
}
ans = Math.max(ans, end - start + 1 );
}
else {
start = end;
end -= 1 ;
sum = 0 ;
}
}
return ans;
}
public static void main(String[] args)
{
int n = 5 , k = 12 ;
int [] a = { 3 , 2 , 4 , 1 , 8 };
int [] b = { 4 , 4 , 2 , 4 , 1 };
System.out.println(maxLengthSubarray(n, a, b, k));
}
}
|
Python3
def max_length_common_subarray(n, a, b, k):
start = 0
end = 0
window_sum = 0
ans = 0
for end in range (n):
if start = = end or b[end - 1 ] % b[end] = = 0 :
window_sum + = a[end]
while window_sum > k:
window_sum - = a[start]
start + = 1
ans = max (ans, end - start + 1 )
else :
start = end
end - = 1
window_sum = 0
return ans
n = 5
k = 12
a = [ 3 , 2 , 4 , 1 , 8 ]
b = [ 4 , 4 , 2 , 4 , 1 ]
print (max_length_common_subarray(n, a, b, k))
|
C#
using System;
public class GFG
{
public static long MaxLengthSubarray( int n, int [] a, int [] b, int k)
{
int start = 0, end;
int sum = 0;
int ans = 0;
for (end = 0; end < n; end++)
{
if (start == end || b[end - 1] % b[end] == 0)
{
sum += a[end];
while (sum > k)
{
sum -= a[start++];
}
ans = Math.Max(ans, end - start + 1);
}
else
{
start = end;
end -= 1;
sum = 0;
}
}
return ans;
}
public static void Main( string [] args)
{
int n = 5, k = 12;
int [] a = { 3, 2, 4, 1, 8 };
int [] b = { 4, 4, 2, 4, 1 };
Console.WriteLine(MaxLengthSubarray(n, a, b, k));
}
}
|
Javascript
function maxLengthSubarray(n, a, b, k) {
let start = 0;
let end = 0;
let sum = 0;
let ans = 0;
for (end = 0; end < n; end++) {
if (start === end || b[end - 1] % b[end] === 0) {
sum += a[end];
while (sum > k) {
sum -= a[start++];
}
ans = Math.max(ans, end - start + 1);
} else {
start = end;
end -= 1;
sum = 0;
}
}
return ans;
}
const n = 5;
const k = 12;
const a = [3, 2, 4, 1, 8];
const b = [4, 4, 2, 4, 1];
console.log(maxLengthSubarray(n, a, b, k));
|
Time Complexity: O(N), where N is the size of array A[] and B[]
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...