📌  相关文章
📜  元素计数,使其与 X 的和/差也存在于数组中

📅  最后修改于: 2021-10-27 06:54:25             🧑  作者: Mango

给定一个数组arr[]和一个整数X ,任务是计算数组的元素,使它们存在一个元素arr[i] - X     或者arr[i] + X     在数组中。
例子:

方法:想法是使用哈希映射来检查元素是否在 O(1) 时间内出现在哈希映射中。然后,遍历数组的元素,并为每个元素检查arr[i] - X     或者arr[i] + X     存在于数组中。如果是,则将此类元素的计数加 1。
下面是上述方法的实现:

C++
// C++ implementation to count of
// elements such that its sum/difference
// with X also exists in the Array
 
#include 
 
using namespace std;
 
// Function to find the count of
// elements in the array such that
// element at the difference at X
// is present in the array
void findAns(int arr[], int n, int x)
{
    int ans;
    unordered_set s;
 
    // Loop to insert the elements
    // of the array into the set
    for (int i = 0; i < n; i++)
        s.insert(arr[i]);
 
    ans = 0;
 
    // Loop to iterate over the array
    for (int i = 0; i < n; i++) {
 
        // if any of the elements are there
        // then increase the count variable
        if (s.find(arr[i] + x) != s.end() || s.find(arr[i] - x) != s.end())
            ans++;
    }
    cout << ans;
    return;
}
 
// Driver Code
int main()
{
    int arr[] = { 2, 2, 4, 5, 6 };
    int n = sizeof(arr) / sizeof(int);
    int x = 3;
 
    findAns(arr, n, x);
 
    return 0;
}


Java
// Java implementation to count of
// elements such that its sum/difference
// with X also exists in the Array
import java.util.*;
class GFG{
 
// Function to find the count of
// elements in the array such that
// element at the difference at X
// is present in the array
static void findAns(int arr[],
                    int n, int x)
{
    int ans;
    HashSet s = new HashSet();
 
    // Loop to insert the elements
    // of the array into the set
    for (int i = 0; i < n; i++)
        s.add(arr[i]);
 
    ans = 0;
 
    // Loop to iterate over the array
    for (int i = 0; i < n; i++)
    {
 
        // if any of the elements are there
        // then increase the count variable
        if (s.contains(arr[i] + x) ||
            s.contains(arr[i] - x))
            ans++;
    }
    System.out.print(ans);
    return;
}
 
// Driver Code
public static void main(String[] args)
{
    int arr[] = { 2, 2, 4, 5, 6 };
    int n = arr.length;
    int x = 3;
 
    findAns(arr, n, x);
}
}
 
// This code is contributed by Rajput-Ji


Python3
# Python3 implementation to count of
# elements such that its sum/difference
# with X also exists in the array
 
# Function to find the count of
# elements in the array such that
# element at the difference at X
# is present in the array
def findAns(arr, n, x):
     
    s = set()
     
    # Loop to insert the elements
    # of the array into the set
    for i in range(n):
        s.add(arr[i])
         
    ans = 0
 
    # Loop to iterate over the array
    for i in range(n):
         
        # If any of the elements are there
        # then increase the count variable
        if arr[i] + x in s or arr[i] - x in s:
            ans = ans + 1
 
    print(ans)
 
# Driver Code
arr = [ 2, 2, 4, 5, 6 ]
n = len(arr)
x = 3
 
# Function call
findAns(arr, n, x)
 
# This code is contributed by ishayadav181


C#
// C# implementation to count of
// elements such that its sum/difference
// with X also exists in the Array
using System;
using System.Collections.Generic;
 
class GFG{
 
// Function to find the count of
// elements in the array such that
// element at the difference at X
// is present in the array
static void findAns(int[] arr,
                    int n, int x)
{
    int ans;
    HashSet s = new HashSet();
     
    // Loop to insert the elements
    // of the array into the set
    for(int i = 0; i < n; i++)
       s.Add(arr[i]);
     
    ans = 0;
     
    // Loop to iterate over the array
    for(int i = 0; i < n; i++)
    {
        
       // if any of the elements are there
       // then increase the count variable
       if (s.Contains(arr[i] + x) ||
           s.Contains(arr[i] - x))
           ans++;
    }
    Console.Write(ans);
    return;
}
     
// Driver Code
public static void Main(String[] args)
{
    int[] arr = { 2, 2, 4, 5, 6 };
    int n = arr.Length;
    int x = 3;
     
    findAns(arr, n, x);
}
}
 
// This code is contributed by ShubhamCoder


Javascript


输出:

3

性能分析:

  • 时间复杂度: O(N)
  • 辅助空间复杂度: O(N)