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 3Input: arr[] = {1, 2, 3, 4, 5}, K = 5
Output: 1
Explanation:
{1, 2, 3, 4, 5}: Pair {1, 5} is divisble 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.
- 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++ implementation of the // above approach #include <bits/stdc++.h> using namespace std;
// Function to return the required // number of subarrays int findSubarrays( int arr[],
int N,
int K)
{ // Return number of possible
// subarrays of length K
return N - K + 1;
} // Driver Code 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 implementation of the // above approach class GFG{
// Function to return the required // number of subarrays static int findSubarrays( int arr[], int N,
int K)
{ // Return number of possible
// subarrays of length K
return N - K + 1 ;
} // Driver Code 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));
} } // This code is contributed by shivanisinghss2110 |
# Python3 implementation of the # above approach # Function to return the required # number of subarrays def findSubarrays(arr, N, K):
# Return number of possible
# subarrays of length K
return N - K + 1 ;
# Driver Code if __name__ = = '__main__' :
arr = [ 1 , 5 , 3 , 2 , 17 , 18 ];
K = 4 ;
N = len (arr);
print (findSubarrays(arr, N, K));
# This code is contributed by Rohit_ranjan |
// C# implementation of the // above approach using System;
class GFG{
// Function to return the required // number of subarrays static int findSubarrays( int []arr, int N,
int K)
{ // Return number of possible
// subarrays of length K
return N - K + 1;
} // Driver Code 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));
} } // This code is contributed by Amit Katiyar |
3
Time complexity: O(1)
Auxiliary Space: O(1)
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- Count all disjoint pairs having absolute difference at least K from a given array
- Pair of integers having least GCD among all given pairs having GCD exceeding K
- Maximum absolute difference between sum of subarrays of size K
- Count subarrays having each distinct element occuring at least twice
- Find a pair with sum N having minimum absolute difference
- Length of longest subsequence having absolute difference of all pairs divisible by K
- Maximize count of subsets having product of smallest element and size of the subset at least X
- Count array elements having at least one smaller element on its left and right side
- Number of subarrays having absolute sum greater than K | Set-2
- Check if all subarrays contains at least one unique element
- Count of subarrays of size K with elements having even frequencies
- Print array elements that are divisible by at-least one other
- Count pairs in an array whose absolute difference is divisible by K
- First subarray having sum at least half the maximum sum of any subarray of size K
- Find elements in a given range having at least one odd divisor
- Sub-strings that start and end with one character and have at least one other
- Count pairs (p, q) such that p occurs in array at least q times and q occurs at least p times
- Count of numbers upto N having absolute difference of at most K between any two adjacent digits
- Maximum subarray size, such that all subarrays of that size have sum less than k
- Find an array of size N having exactly K subarrays with sum S
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.