Count of longest possible subarrays with sum not divisible by K
Given an array of integers arr[] and a positive integer K, the task is to find the count of the longest possible subarrays with sum of its elements not divisible by K.
Examples:
Input: arr[] = {2, 3, 4, 6}, K = 3
Output: 1
Explanation: There is only one longest possible subarray of size 3 i.e. {3, 4, 6} having a sum 13, which is not divisible by K = 3.
Input: arr[] = {2, 4, 3, 5, 1}, K = 3
Output: 2
Explanation: There are 2 longest possible subarrays of size 4 i.e. {2, 4, 3, 5} and {4, 3, 5, 1} having a sum 14 and 13 respectively, which is not divisible by K = 3.
Approach:
- Check if the sum of all the elements of the array is divisible by K
- If the sum is not divisible by K, return 1 as the longest subarray would be of size N.
- Else
- Find the index of the first number not divisible by K. Let that be L.
- Find the index of the last number not divisible by K. Let that be R.
- Remove the elements all the way up to index L and find the size of the subarray. Remove the elements beyond R and find the size of this subarray as well. Whichever length is greater, that will be the size of the longest subarray not divisible by K.
- Using this length as the window size, apply the sliding window technique on the arr[] to find out the count of sub-arrays of the size obtained above which are not divisible by K.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int CountLongestSubarrays(
int arr[], int n, int k)
{
int i, s = 0;
for (i = 0; i < n; ++i) {
s += arr[i];
}
if (s % k) {
return 1;
}
else {
int ini = 0;
while (ini < n
&& arr[ini] % k == 0) {
++ini;
}
int final = n - 1;
while (final >= 0
&& arr[final] % k == 0) {
--final;
}
int len, sum = 0, count = 0;
if (ini == n) {
return -1;
}
else {
len = max(n - 1 - ini,
final);
}
for (i = 0; i < len; i++) {
sum += arr[i];
}
if (sum % k != 0) {
count++;
}
for (i = len; i < n; i++) {
sum = sum + arr[i];
sum = sum - arr[i - len];
if (sum % k != 0) {
count++;
}
}
return count;
}
}
int main()
{
int arr[] = { 3, 2, 2, 2, 3 };
int n = sizeof (arr)
/ sizeof (arr[0]);
int k = 3;
cout << CountLongestSubarrays(arr, n, k);
return 0;
}
|
Java
import java.util.*;
class GFG{
static int CountLongestSubarrays( int arr[],
int n, int k)
{
int i, s = 0 ;
for (i = 0 ; i < n; ++i)
{
s += arr[i];
}
if ((s % k) != 0 )
{
return 1 ;
}
else
{
int ini = 0 ;
while (ini < n && arr[ini] % k == 0 )
{
++ini;
}
int fin = n - 1 ;
while (fin >= 0 && arr[fin] % k == 0 )
{
--fin;
}
int len, sum = 0 , count = 0 ;
if (ini == n)
{
return - 1 ;
}
else
{
len = Math.max(n - 1 - ini, fin);
}
for (i = 0 ; i < len; i++)
{
sum += arr[i];
}
if (sum % k != 0 )
{
count++;
}
for (i = len; i < n; i++)
{
sum = sum + arr[i];
sum = sum - arr[i - len];
if (sum % k != 0 )
{
count++;
}
}
return count;
}
}
public static void main (String []args)
{
int arr[] = { 3 , 2 , 2 , 2 , 3 };
int n = arr.length;
int k = 3 ;
System.out.print(CountLongestSubarrays(
arr, n, k));
}
}
|
Python3
def CountLongestSubarrays(arr, n, k):
s = 0
for i in range (n):
s + = arr[i]
if (s % k):
return 1
else :
ini = 0
while (ini < n and arr[ini] % k = = 0 ):
ini + = 1
final = n - 1
while (final > = 0 and arr[final] % k = = 0 ):
final - = 1
sum , count = 0 , 0
if (ini = = n):
return - 1
else :
length = max (n - 1 - ini, final)
for i in range (length):
sum + = arr[i]
if ( sum % k ! = 0 ):
count + = 1
for i in range (length, n):
sum = sum + arr[i]
sum = sum + arr[i - length]
if ( sum % k ! = 0 ):
count + = 1
return count
if __name__ = = '__main__' :
arr = [ 3 , 2 , 2 , 2 , 3 ]
n = len (arr)
k = 3
print (CountLongestSubarrays(arr, n, k))
|
C#
using System;
class GFG{
static int CountLongestSubarrays( int [] arr,
int n, int k)
{
int i, s = 0;
for (i = 0; i < n; ++i)
{
s += arr[i];
}
if ((s % k) != 0)
{
return 1;
}
else
{
int ini = 0;
while (ini < n && arr[ini] % k == 0)
{
++ini;
}
int fin = n - 1;
while (fin >= 0 && arr[fin] % k == 0)
{
--fin;
}
int len, sum = 0, count = 0;
if (ini == n)
{
return -1;
}
else
{
len = Math.Max(n - 1 - ini, fin);
}
for (i = 0; i < len; i++)
{
sum += arr[i];
}
if (sum % k != 0)
{
count++;
}
for (i = len; i < n; i++)
{
sum = sum + arr[i];
sum = sum - arr[i - len];
if (sum % k != 0)
{
count++;
}
}
return count;
}
}
public static void Main(String[] args)
{
int [] arr = { 3, 2, 2, 2, 3 };
int n = arr.Length;
int k = 3;
Console.WriteLine(CountLongestSubarrays(
arr, n, k));
}
}
|
Javascript
<script>
function CountLongestSubarrays(arr, n, k)
{
let i, s = 0;
for (i = 0; i < n; ++i)
{
s += arr[i];
}
if ((s % k) != 0)
{
return 1;
}
else
{
let ini = 0;
while (ini < n && arr[ini] % k == 0)
{
++ini;
}
let fin = n - 1;
while (fin >= 0 && arr[fin] % k == 0)
{
--fin;
}
let len, sum = 0, count = 0;
if (ini == n)
{
return -1;
}
else
{
len = Math.max(n - 1 - ini, fin);
}
for (i = 0; i < len; i++)
{
sum += arr[i];
}
if (sum % k != 0)
{
count++;
}
for (i = len; i < n; i++)
{
sum = sum + arr[i];
sum = sum - arr[i - len];
if (sum % k != 0)
{
count++;
}
}
return count;
}
}
let arr = [ 3, 2, 2, 2, 3 ];
let n = arr.length;
let k = 3;
document.write(CountLongestSubarrays(
arr, n, k));
</script>
|
Time Complexity: O(N)
Auxiliary Space Complexity: O(1)
Last Updated :
12 May, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...