📌  相关文章
📜  用第二个数组中的元素对第一个数组中的绝对差大于K的元素进行计数

📅  最后修改于: 2021-06-27 01:40:00             🧑  作者: Mango

给定两个数组arr1 []和arr2 []和一个整数K ,我们的任务是在第一个数组中找到数字元素,对于arr1 []中的元素x,在arr2 []中至少存在一个元素y。使得x和y的绝对差大于整数K。

例子:

天真的方法:对arr1 []中的每个元素进行迭代,并检查arr2中是否存在一个元素,以使它们的绝对差大于值K。

时间复杂度: O(N * M)其中N和M分别是数组1和2的大小。

高效方法:要优化上述方法,我们必须观察到arr1 []中的每个元素,仅需要arr2 []最小和最大元素即可检查其是否远离。对于每个元素x,在arr1中,如果最小值或最大值与x的绝对差大于K,则该元素较远。

下面是上述方法的实现:

C++
// C++ program to count elements in first Array
// with absolute difference greater than K
// with an element in second Array
 
#include 
using namespace std;
 
// Function to count the such elements
void countDist(int arr1[], int n, int arr2[],
               int m, int k)
{
    // Store count of required elements in arr1
    int count = 0;
 
    // Initialise the smallest and the largest
    // value from the second array arr2[]
    int smallest = arr2[0];
    int largest = arr2[0];
 
    // Find the smallest and
    // the largest element in arr2
    for (int i = 0; i < m; i++) {
        smallest = max(smallest, arr2[i]);
        largest = min(largest, arr1[i]);
    }
    for (int i = 0; i < n; i++) {
 
        // Check if absolute difference of smallest
        // and arr1[i] or largest and arr1[i] is > K
        // then arr[i] is a required element
        if (abs(arr1[i] - smallest) > k
            || abs(arr1[i] - largest) > k)
            count++;
    }
 
    // Print the final result
    cout << count;
}
 
// Driver code
int main()
{
    int arr1[] = { 3, 1, 4 };
    int n = sizeof(arr1) / sizeof(arr1[0]);
    int arr2[] = { 5, 1, 2 };
    int m = sizeof(arr2) / sizeof(arr2[0]);
    int k = 2;
 
    countDist(arr1, n, arr2, m, k);
 
    return 0;
}


Java
// Java program to count elements in first Array
// with absolute difference greater than K
// with an element in second Array
class GFG{
 
// Function to count the such elements
static void countDist(int arr1[], int n,
                      int arr2[], int m,
                      int k)
{
    // Store count of required elements in arr1
    int count = 0;
 
    // Initialise the smallest and the largest
    // value from the second array arr2[]
    int smallest = arr2[0];
    int largest = arr2[0];
 
    // Find the smallest and
    // the largest element in arr2
    for(int i = 0; i < m; i++)
    {
       smallest = Math.max(smallest, arr2[i]);
       largest = Math.min(largest, arr1[i]);
    }
    for(int i = 0; i < n; i++)
    {
 
       // Check if absolute difference
       // of smallest and arr1[i] or
       // largest and arr1[i] is > K
       // then arr[i] is a required element
       if (Math.abs(arr1[i] - smallest) > k ||
           Math.abs(arr1[i] - largest) > k)
           count++;
    }
 
    // Print the final result
    System.out.print(count);
}
 
// Driver code
public static void main(String[] args)
{
    int arr1[] = { 3, 1, 4 };
    int n = arr1.length;
    int arr2[] = { 5, 1, 2 };
    int m = arr2.length;
    int k = 2;
 
    countDist(arr1, n, arr2, m, k);
}
}
 
// This code is contributed by 29AjayKumar


Python3
# Python3 program to count elements in the first Array
# with an absolute difference greater than K
# with an element in the second Array
 
# Function to count the such elements
def countDist(arr1, n, arr2, m, k):
     
    # Store count of required elements in arr1
    count = 0
 
    # Initialise the smallest and the largest
    # value from the second array arr2[]
    smallest = arr2[0]
    largest = arr2[0]
 
    # Find the smallest and
    # the largest element in arr2
    for i in range(m):
        smallest = max(smallest, arr2[i])
        largest = min(largest, arr1[i])
 
    for i in range(n):
 
        # Check if absolute difference of smallest
        # and arr1[i] or largest and arr1[i] is > K
        # then arr[i] is a required element
        if (abs(arr1[i] - smallest) > k
            or abs(arr1[i] - largest) > k):
            count += 1
 
    # Print final result
    print(count)
 
 
# Driver code
if __name__ == '__main__':
     
    arr1= [ 3, 1, 4 ]
    n = len(arr1)
    arr2= [ 5, 1, 2 ]
    m = len(arr2)
    k = 2
 
    countDist(arr1, n, arr2, m, k)
     
# This code is contributed by mohit kumar 29


C#
// C# program to count elements in first array
// with absolute difference greater than K
// with an element in second Array
using System;
 
class GFG{
 
// Function to count the such elements
static void countDist(int []arr1, int n,
                      int []arr2, int m,
                      int k)
{
    // Store count of required elements in arr1
    int count = 0;
 
    // Initialise the smallest and the largest
    // value from the second array arr2[]
    int smallest = arr2[0];
    int largest = arr2[0];
 
    // Find the smallest and
    // the largest element in arr2
    for(int i = 0; i < m; i++)
    {
       smallest = Math.Max(smallest, arr2[i]);
       largest = Math.Min(largest, arr1[i]);
    }
    for(int i = 0; i < n; i++)
    {
        
       // Check if absolute difference
       // of smallest and arr1[i] or
       // largest and arr1[i] is > K
       // then arr[i] is a required element
       if (Math.Abs(arr1[i] - smallest) > k ||
           Math.Abs(arr1[i] - largest) > k)
           count++;
    }
 
    // Print the readonly result
    Console.Write(count);
}
 
// Driver code
public static void Main(String[] args)
{
    int []arr1 = { 3, 1, 4 };
    int n = arr1.Length;
    int []arr2 = { 5, 1, 2 };
    int m = arr2.Length;
    int k = 2;
 
    countDist(arr1, n, arr2, m, k);
}
}
 
// This code is contributed by gauravrajput1


Javascript


输出:
2

时间复杂度: O(N + M) ,其中N和M是给定数组的大小。

如果您希望与行业专家一起参加现场课程,请参阅《 Geeks现场课程》和《 Geeks现场课程美国》。