Count maximum elements of an array whose absolute difference does not exceed K
Given an array A and positive integer K. The task is to find maximum number elements for which the absolute difference of any of the pair does not exceed K.
Examples:
Input: A[] = {1, 26, 17, 12, 15, 2}, K = 5
Output: 3
There are maximum 3 values so that the absolute difference of each pair
does not exceed K(K=5) ie., {12, 15, 17}
Input: A[] = {1, 2, 5, 10, 8, 3}, K = 4
Output: 4
There are maximum 4 values so that the absolute difference of each pair
does not exceed K(K=4) ie., {1, 2, 3, 5}
Approach:
- Sort the given Array in ascending order.
- Iterate from index i = 0 to n.
- For every A[i] count how many values which are in range A[i] to A[i] + K
ie., A[i]<= A[j] <= A[i]+K - Return Max Count
Below is the implementation of the above approach:
C++
// C++ implementation of the above approach #include <bits/stdc++.h> using namespace std; // Function to return the maximum elements // in which absolute difference of any pair // does not exceed K int maxCount(int A[], int N, int K) { int maximum = 0; int i = 0, j = 0; int start = 0; int end = 0; // Sort the Given array sort(A, A + N); // Find max elements for (i = 0; i < N; i++) { // Count all elements which are in range // A[i] to A[i] + K while (j < N && A[j] <= A[i] + K) j++; if (maximum < (j - i)) { maximum = (j - i); start = i; end = j; } } // Return the max count return maximum; } // Driver code int main() { int A[] = { 1, 26, 17, 12, 15, 2 }; int N = sizeof(A) / sizeof(A[0]); int K = 5; cout << maxCount(A, N, K); return 0; } |
Java
// Java implementation of the approach import java.util.*; class GFG { // Function to return the maximum elements // in which absolute difference of any pair // does not exceed K static int maxCount(int A[], int N, int K) { int maximum = 0; int i = 0, j = 0; int start = 0; int end = 0; // Sort the Given array Arrays.sort(A); // Find max elements for (i = 0; i < N; i++) { // Count all elements which are in range // A[i] to A[i] + K while (j < N && A[j] <= A[i] + K) j++; if (maximum < (j - i)) { maximum = (j - i); start = i; end = j; } } // Return the max count return maximum; } // Driver code public static void main(String[] args) { int A[] = { 1, 26, 17, 12, 15, 2 }; int N = A.length; int K = 5; System.out.println(maxCount(A, N, K)); } } // This code has been contributed by 29AjayKumar |
Python3
# Python3 implementation of the approach def maxCount(A, N, K): maximum = 0 start = 0 end = 0 j = 0 # Sort the Array A.sort() # Find max elements for i in range(0, N): while(j < N and A[j] <= A[i] + K): j += 1 if maximum < (j - i ): maximum = (j - i) start = i; end = j; # Return the maximum return maximum # Driver code A = [1, 26, 17, 12, 15, 2] N = len(A) K = 5 print(maxCount(A, N, K)) |
C#
// C# implementation of the approach using System; class GFG { // Function to return the maximum elements // in which absolute difference of any pair // does not exceed K static int maxCount(int []A, int N, int K) { int maximum = 0; int i = 0, j = 0; int start = 0; int end = 0; // Sort the Given array Array.Sort(A); // Find max elements for (i = 0; i < N; i++) { // Count all elements which are in range // A[i] to A[i] + K while (j < N && A[j] <= A[i] + K) j++; if (maximum < (j - i)) { maximum = (j - i); start = i; end = j; } } // Return the max count return maximum; } // Driver code public static void Main() { int []A = { 1, 26, 17, 12, 15, 2 }; int N = A.Length; int K = 5; Console.Write(maxCount(A, N, K)); } } /* This code contributed by PrinciRaj1992 */ |
PHP
<?php // PHP implementation of the above approach // Function to return the maximum // elements in which absolute difference // of any pair does not exceed K function maxCount($A, $N, $K) { $maximum = 0; $i = 0; $j = 0; $start = 0; $end = 0; // Sort the Given array sort($A); // Find max elements for ($i = 0; $i < $N; $i++) { // Count all elements which // are in range A[i] to A[i] + K while ($j < $N && $A[$j] <= $A[$i] + $K) $j++; if ($maximum < ($j - $i)) { $maximum = ($j - $i); $start = $i; $end = $j; } } // Return the max count return $maximum; } // Driver code $A = array( 1, 26, 17, 12, 15, 2 ); $N = Count($A); $K = 5; echo maxCount($A, $N, $K); // This code is contributed // by Arnab Kundu ?> |
3
Recommended Posts:
- Count of elements whose absolute difference with the sum of all the other elements is greater than k
- Find maximum number of elements such that their absolute difference is less than or equal to 1
- Maximum absolute difference in an array
- Maximum sum of absolute difference of an array
- Count pairs in an array such that the absolute difference between them is ≥ K
- k-th smallest absolute difference of two elements in an array
- Absolute Difference of even and odd indexed elements in an Array
- Absolute Difference of all pairwise consecutive elements in an array
- Minimum absolute difference of adjacent elements in a circular array
- Pick maximum sum M elements such that contiguous repetitions do not exceed K
- Maximum difference between group of k-elements and rest of the array.
- Choose k array elements such that difference of maximum and minimum is minimized
- Find the node whose absolute difference with X gives maximum value
- Maximum absolute difference between sum of two contiguous sub-arrays
- Maximum absolute difference of value and index sums
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.



