📌  相关文章
📜  计算绝对差不超过K的数组的最大元素

📅  最后修改于: 2021-04-27 20:01:28             🧑  作者: Mango

给定数组A和正整数K。任务是找到任何一对中的绝对差不超过K的最大元素个数。

例子:

方法:

  1. 以升序对给定数组进行排序。
  2. 从索引i = 0迭代到n。
  3. 对于每个A [i]计数多少个A [i]到A [i] + K范围内的值
    即,A [i] <= A [j] <= A [i] + K
  4. 返回最大计数

下面是上述方法的实现:

C++
// C++ implementation of the above approach
  
#include 
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


输出:
3