Given an arr[] consisting of N elements, the task is to count all subarrays of size K having atleast one pair whose absolute difference is divisible by K – 1.
Examples:
Input: arr[] = {1, 5, 3, 2, 17, 18}, K = 4
Output: 3
Explanation:
The three subarrays of size 4 are:
{1, 5, 3, 2}: Pair {5, 2} have difference divisible by 3
{5, 3, 2, 17}: Pairs {5, 2}, {5, 17}, {2, 17} have difference divisible by 3
{3, 2, 17, 18}: Pairs {3, 18}, {2, 17} have difference divisible by 3
Input: arr[] = {1, 2, 3, 4, 5}, K = 5
Output: 1
Explanation:
{1, 2, 3, 4, 5}: Pair {1, 5} is divisible by 4
Naive Approach:
The simplest approach to solve the problem is to iterate over all subarrays of size K and check if there exists any pair whose difference is divisible by K – 1.
Time Complexity: O(N * K * K)
Efficient Approach: The above approach can be optimized using Pigeonhole Principle. Follow the steps below to solve the problem:
- Consider K-1 boxes labeled 0, 1, 2, …, K-2 respectively. They represent the remainders when any number x from the array is divided by K-1, which means the boxes store the modulo K-1 of array elements.
- Now, in a subarray of size K, according to the Pigeonhole Principle, there must be atleast one pair of boxes with same remainders. It means that there is atleast one pair whose difference or even the summation will be divisible by K-1
- From this theorem we can conclude that every subarray of size K, will always have atleast one pair whose difference is divisible by K-1.
- So, the answer will be equal to the number of subarrays of size K possible from the given array, which is equal to N – K + 1.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int findSubarrays( int arr[],
int N,
int K)
{
return N - K + 1;
}
int main()
{
int arr[] = { 1, 5, 3, 2, 17, 18 };
int K = 4;
int N = sizeof (arr) / sizeof (arr[0]);
cout << findSubarrays(arr, N, K);
return 0;
}
|
Java
class GFG{
static int findSubarrays( int arr[], int N,
int K)
{
return N - K + 1 ;
}
public static void main(String[] args)
{
int arr[] = { 1 , 5 , 3 , 2 , 17 , 18 };
int K = 4 ;
int N = arr.length;
System.out.print(findSubarrays(arr, N, K));
}
}
|
Python3
def findSubarrays(arr, N, K):
return N - K + 1 ;
if __name__ = = '__main__' :
arr = [ 1 , 5 , 3 , 2 , 17 , 18 ];
K = 4 ;
N = len (arr);
print (findSubarrays(arr, N, K));
|
C#
using System;
class GFG{
static int findSubarrays( int []arr, int N,
int K)
{
return N - K + 1;
}
public static void Main(String[] args)
{
int []arr = { 1, 5, 3, 2, 17, 18 };
int K = 4;
int N = arr.Length;
Console.Write(findSubarrays(arr, N, K));
}
}
|
Javascript
<script>
function findSubarrays(arr, N, K)
{
return N - K + 1;
}
let arr = [ 1, 5, 3, 2, 17, 18 ];
let K = 4;
let N = arr.length;
document.write(findSubarrays(arr, N, K));
</script>
|
Time complexity: O(1)
Auxiliary Space: O(1)
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!
Last Updated :
08 Nov, 2022
Like Article
Save Article