Length of longest subarray whose sum is not divisible by integer K
Given an array arr[] of size N and an integer k, our task is to find the length of longest subarray whose sum of elements is not divisible by k. If no such subarray exists then return -1.
Examples:
Input: arr[] = {8, 4, 3, 1, 5, 9, 2}, k = 2
Output: 5
Explanation:
The subarray is {8, 4, 3, 1, 5} with sum = 21, is not divisible by 2.
Input: arr[] = {6, 3, 12, 15}, k = 3
Output: -1
Explanation:
There is no subarray which is not divisible by 3.
Naive Approach: The idea is to consider all the subarrays and return the length of the longest subarray such that the sum of its elements is not divisible by k.
Time Complexity: O(N2)
Auxiliary Space: O(N)
Efficient Approach: The main observation is that removing an element that is divisible by k will not contribute to the solution, but if we remove an element that is not divisible by k then the sum would not be divisible by k.
- Therefore, let the leftmost non-multiple of k be at index left, and the rightmost non-multiple of k be at index right.
- Remove either the prefix elements up to index left, or the suffix element up to index right and remove the elements which have lesser number of elements.
- There are two corner cases in this problem. First is, if every element of the array are divisible by k, then no such subarray exist so return -1. Secondly, if sum of the whole array is not divisible by k, then the subarray will be the array itself, so return the size of the array.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int MaxSubarrayLength( int arr[], int n, int k)
{
int left = -1;
int right;
int sum = 0;
for ( int i = 0; i < n; i++) {
if ((arr[i] % k) != 0) {
if (left == -1) {
left = i;
}
right = i;
}
sum += arr[i];
}
if ((sum % k) != 0) {
return n;
}
else if (left == -1) {
return -1;
}
else {
int prefix_length = left + 1;
int suffix_length = n - right;
return n - min(prefix_length,
suffix_length);
}
}
int main()
{
int arr[] = { 6, 3, 12, 15 };
int n = sizeof (arr) / sizeof (arr[0]);
int K = 3;
cout << MaxSubarrayLength(arr, n, K);
return 0;
}
|
Java
import java.util.*;
class GFG{
static int MaxSubarrayLength( int arr[], int n,
int k)
{
int left = - 1 ;
int right = 0 ;
int sum = 0 ;
for ( int i = 0 ; i < n; i++)
{
if ((arr[i] % k) != 0 )
{
if (left == - 1 )
{
left = i;
}
right = i;
}
sum += arr[i];
}
if ((sum % k) != 0 )
{
return n;
}
else if (left == - 1 )
{
return - 1 ;
}
else
{
int prefix_length = left + 1 ;
int suffix_length = n - right;
return n - Math.min(prefix_length,
suffix_length);
}
}
public static void main(String[] args)
{
int arr[] = { 6 , 3 , 12 , 15 };
int n = arr.length;
int K = 3 ;
System.out.println(MaxSubarrayLength(arr, n, K));
}
}
|
Python3
def MaxSubarrayLength(arr, n, k):
left = - 1
sum = 0
for i in range (n):
if ((arr[i] % k) ! = 0 ):
if (left = = - 1 ):
left = i
right = i
sum + = arr[i]
if (( sum % k) ! = 0 ):
return n
elif (left = = - 1 ):
return - 1
else :
prefix_length = left + 1
suffix_length = n - right
return n - min (prefix_length,
suffix_length)
if __name__ = = "__main__" :
arr = [ 6 , 3 , 12 , 15 ]
n = len (arr)
K = 3
print (MaxSubarrayLength(arr, n, K))
|
C#
using System;
class GFG{
static int MaxSubarrayLength( int []arr, int n,
int k)
{
int left = -1;
int right = 0;
int sum = 0;
for ( int i = 0; i < n; i++)
{
if ((arr[i] % k) != 0)
{
if (left == -1)
{
left = i;
}
right = i;
}
sum += arr[i];
}
if ((sum % k) != 0)
{
return n;
}
else if (left == -1)
{
return -1;
}
else
{
int prefix_length = left + 1;
int suffix_length = n - right;
return n - Math.Min(prefix_length,
suffix_length);
}
}
public static void Main( string [] args)
{
int []arr = { 6, 3, 12, 15 };
int n = arr.Length;
int K = 3;
Console.Write(MaxSubarrayLength(arr, n, K));
}
}
|
Javascript
<script>
function MaxSubarrayLength(arr, n, k)
{
let left = -1;
let right = 0;
let sum = 0;
for (let i = 0; i < n; i++)
{
if ((arr[i] % k) != 0)
{
if (left == -1)
{
left = i;
}
right = i;
}
sum += arr[i];
}
if ((sum % k) != 0)
{
return n;
}
else if (left == -1)
{
return -1;
}
else
{
let prefix_length = left + 1;
let suffix_length = n - right;
return n - Math.min(prefix_length,
suffix_length);
}
}
let arr = [ 6, 3, 12, 15 ];
let n = arr.length;
let K = 3;
document.write(MaxSubarrayLength(arr, n, K));
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Related Topic: Subarrays, Subsequences, and Subsets in Array
Last Updated :
11 Jul, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...