Length of longest subarray whose sum is not divisible by integer K
Last Updated :
11 Jul, 2022
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++
// C++ Program to find the length of
// the longest subarray whose sum is
// not divisible by integer K
#include <bits/stdc++.h>
using namespace std;
// Function to find the longest subarray
// with sum is not divisible by k
int MaxSubarrayLength(int arr[], int n, int k)
{
// left is the index of the
// leftmost element that is
// not divisible by k
int left = -1;
// right is the index of the
// rightmost element that is
// not divisible by k
int right;
// sum of the array
int sum = 0;
for (int i = 0; i < n; i++) {
// Find the element that
// is not multiple of k
if ((arr[i] % k) != 0) {
// left = -1 means we are
// finding the leftmost
// element that is not
// divisible by k
if (left == -1) {
left = i;
}
// Updating the
// rightmost element
right = i;
}
// update the sum of the
// array up to the index i
sum += arr[i];
}
// Check if the sum of the
// array is not divisible
// by k, then return the
// size of array
if ((sum % k) != 0) {
return n;
}
// All elements of array
// are divisible by k,
// then no such subarray
// possible so return -1
else if (left == -1) {
return -1;
}
else {
// length of prefix elements
// that can be removed
int prefix_length = left + 1;
// length of suffix elements
// that can be removed
int suffix_length = n - right;
// Return the length of
// subarray after removing
// the elements which have
// lesser number of elements
return n - min(prefix_length,
suffix_length);
}
}
// Driver Code
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
// Java program to find the length of
// the longest subarray whose sum is
// not divisible by integer K
import java.util.*;
class GFG{
// Function to find the longest subarray
// with sum is not divisible by k
static int MaxSubarrayLength(int arr[], int n,
int k)
{
// left is the index of the
// leftmost element that is
// not divisible by k
int left = -1;
// right is the index of the
// rightmost element that is
// not divisible by k
int right = 0;
// sum of the array
int sum = 0;
for(int i = 0; i < n; i++)
{
// Find the element that
// is not multiple of k
if ((arr[i] % k) != 0)
{
// left = -1 means we are
// finding the leftmost
// element that is not
// divisible by k
if (left == -1)
{
left = i;
}
// Updating the
// rightmost element
right = i;
}
// Update the sum of the
// array up to the index i
sum += arr[i];
}
// Check if the sum of the
// array is not divisible
// by k, then return the
// size of array
if ((sum % k) != 0)
{
return n;
}
// All elements of array
// are divisible by k,
// then no such subarray
// possible so return -1
else if (left == -1)
{
return -1;
}
else
{
// Length of prefix elements
// that can be removed
int prefix_length = left + 1;
// Length of suffix elements
// that can be removed
int suffix_length = n - right;
// Return the length of
// subarray after removing
// the elements which have
// lesser number of elements
return n - Math.min(prefix_length,
suffix_length);
}
}
// Driver code
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));
}
}
// This code is contributed by offbeat
Python3
# Python3 program to find the length of
# the longest subarray whose sum is
# not divisible by integer
# Function to find the longest subarray
# with sum is not divisible by k
def MaxSubarrayLength(arr, n, k):
# left is the index of the
# leftmost element that is
# not divisible by k
left = -1
# sum of the array
sum = 0
for i in range(n):
# Find the element that
# is not multiple of k
if ((arr[i] % k) != 0):
# left = -1 means we are
# finding the leftmost
# element that is not
# divisible by k
if (left == -1):
left = i
# Updating the
# rightmost element
right = i
# Update the sum of the
# array up to the index i
sum += arr[i]
# Check if the sum of the
# array is not divisible
# by k, then return the
# size of array
if ((sum % k) != 0):
return n
# All elements of array
# are divisible by k,
# then no such subarray
# possible so return -1
elif(left == -1):
return -1
else:
# length of prefix elements
# that can be removed
prefix_length = left + 1
# length of suffix elements
# that can be removed
suffix_length = n - right
# Return the length of
# subarray after removing
# the elements which have
# lesser number of elements
return n - min(prefix_length,
suffix_length)
# Driver Code
if __name__ == "__main__":
arr = [ 6, 3, 12, 15 ]
n = len(arr)
K = 3
print(MaxSubarrayLength(arr, n, K))
# This code is contributed by chitranayal
C#
// C# program to find the length of
// the longest subarray whose sum is
// not divisible by integer K
using System;
class GFG{
// Function to find the longest subarray
// with sum is not divisible by k
static int MaxSubarrayLength(int []arr, int n,
int k)
{
// left is the index of the
// leftmost element that is
// not divisible by k
int left = -1;
// right is the index of the
// rightmost element that is
// not divisible by k
int right = 0;
// sum of the array
int sum = 0;
for(int i = 0; i < n; i++)
{
// Find the element that
// is not multiple of k
if ((arr[i] % k) != 0)
{
// left = -1 means we are
// finding the leftmost
// element that is not
// divisible by k
if (left == -1)
{
left = i;
}
// Updating the
// rightmost element
right = i;
}
// Update the sum of the
// array up to the index i
sum += arr[i];
}
// Check if the sum of the
// array is not divisible
// by k, then return the
// size of array
if ((sum % k) != 0)
{
return n;
}
// All elements of array
// are divisible by k,
// then no such subarray
// possible so return -1
else if (left == -1)
{
return -1;
}
else
{
// Length of prefix elements
// that can be removed
int prefix_length = left + 1;
// Length of suffix elements
// that can be removed
int suffix_length = n - right;
// Return the length of
// subarray after removing
// the elements which have
// lesser number of elements
return n - Math.Min(prefix_length,
suffix_length);
}
}
// Driver code
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));
}
}
// This code is contributed by rutvik_56
JavaScript
<script>
// Javascript program to find the length of
// the longest subarray whose sum is
// not divisible by integer K
// Function to find the longest subarray
// with sum is not divisible by k
function MaxSubarrayLength(arr, n, k)
{
// left is the index of the
// leftmost element that is
// not divisible by k
let left = -1;
// right is the index of the
// rightmost element that is
// not divisible by k
let right = 0;
// sum of the array
let sum = 0;
for(let i = 0; i < n; i++)
{
// Find the element that
// is not multiple of k
if ((arr[i] % k) != 0)
{
// left = -1 means we are
// finding the leftmost
// element that is not
// divisible by k
if (left == -1)
{
left = i;
}
// Updating the
// rightmost element
right = i;
}
// Update the sum of the
// array up to the index i
sum += arr[i];
}
// Check if the sum of the
// array is not divisible
// by k, then return the
// size of array
if ((sum % k) != 0)
{
return n;
}
// All elements of array
// are divisible by k,
// then no such subarray
// possible so return -1
else if (left == -1)
{
return -1;
}
else
{
// Length of prefix elements
// that can be removed
let prefix_length = left + 1;
// Length of suffix elements
// that can be removed
let suffix_length = n - right;
// Return the length of
// subarray after removing
// the elements which have
// lesser number of elements
return n - Math.min(prefix_length,
suffix_length);
}
}
// Driver Code
let arr = [ 6, 3, 12, 15 ];
let n = arr.length;
let K = 3;
document.write(MaxSubarrayLength(arr, n, K));
// This code is contributed by target_2.
</script>
Time Complexity: O(N)
Auxiliary Space: O(1)
Related Topic: Subarrays, Subsequences, and Subsets in Array
Similar Reads
Maximum length of subarray such that sum of the subarray is even Given an array of N elements. The task is to find the length of the longest subarray such that sum of the subarray is even.Examples: Input : N = 6, arr[] = {1, 2, 3, 2, 1, 4}Output : 5Explanation: In the example the subarray in range [2, 6] has sum 12 which is even, so the length is 5.Input : N = 4,
11 min read
Length of the longest subarray whose Bitwise XOR is K Given an array arr[] of size N and an integer K, the task is to find the length of the longest subarray having Bitwise XOR of all its elements equal to K. Examples: Input: arr[] = { 1, 2, 4, 7, 2 }, K = 1Output: 3Explanation: Subarray having Bitwise XOR equal to K(= 1) are { { 1 }, { 2, 4, 7 }, { 1
11 min read
Max sum of Subarray of length N/2 where sum is divisible by N Given an array, arr[] of size N where N is even, the task is to find the maximum possible sum of the subarray of length N/2 where the sum is divisible by N. Examples: Input: arr[] = {2, 3, 4, 5, 6, 7}, N = 6Output:18Explanation: Here, N = 6, The maximum possible sum of a sublist of length 3 (N/2) is
8 min read
Longest subarray with sum not divisible by X Given an array arr[] and an integer X, the task is to print the longest subarray such that the sum of its elements isn't divisible by X. If no such subarray exists, print "-1". Note: If more than one subarray exists with the given property, print any one of them.Examples: Input: arr[] = {1, 2, 3} X
15+ min read
Longest Subarray With Sum Divisible By K Given an arr[] containing n integers and a positive integer k, he problem is to find the longest subarray's length with the sum of the elements divisible by k.Examples:Input: arr[] = [2, 7, 6, 1, 4, 5], k = 3Output: 4Explanation: The subarray [7, 6, 1, 4] has sum = 18, which is divisible by 3.Input:
10 min read