Given an array **arr[]** and an integer **X**, the task is to count the elements of the array such that their exist a element or in the array.

**Examples:**

Input:arr[] = {3, 4, 2, 5}, X = 2

Output:4

Explanation:

In the above-given example, there are 4 such numbers –

For Element 3: Possible numbers are 1, 5, whereas 5 is present in array

For Element 4: Possible numbers are 2, 6, whereas 2 is present in array

For Element 2: Possible numbers are 0, 4, whereas 4 is present in array

For Element 5: Possible numbers are 3, 7, whereas 3 is present in array

Therefore, Total count = 4

Input:arr[] = {2, 2, 4, 5, 6}, X = 3

Output:3

Explanation:

In the above-given example, there are 3 such numbers {2, 2, 5}

**Approach:** The idea is to use hash-map to check that an element is present in the hash-map or not in O(1) time. Then, iterate over the elements of the array and for each element check that or is present in the array. If yes, then increment the count of such elements by 1.

Below is the implemenation of the above approach:

## C++

`// C++ implementation to count of ` `// elements such that its sum/difference ` `// with X also exists in the Array ` ` ` `#include <bits/stdc++.h> ` ` ` `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<` `int` `> 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 increse 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; ` `} ` |

*chevron_right*

*filter_none*

## 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<Integer> s = ` `new` `HashSet<Integer>(); ` ` ` ` ` `// 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 increse 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 ` |

*chevron_right*

*filter_none*

## 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 ` |

*chevron_right*

*filter_none*

## 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<` `int` `> s = ` `new` `HashSet<` `int` `>(); ` ` ` ` ` `// 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 increse 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 ` |

*chevron_right*

*filter_none*

**Output:**

3

**Performance Analysis:**

**Time Complexity:**O(N)**Auxiliary Space Complexity:**O(N)

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:

- Find the number of elements X such that X + K also exists in the array
- Count number of distinct pairs whose sum exists in the given array
- Count of Array elements greater than all elements on its left and at least K elements on its right
- Count of Array elements to be divided by 2 to make at least K elements equal
- Count of array elements which is smaller than both its adjacent elements
- Count of array elements which are greater than all elements on its left
- Count of elements not divisible by any other elements of Array
- Count number of elements between two given elements in array
- Count array elements that divide the sum of all other elements
- Count elements in first Array with absolute difference greater than K with an element in second Array
- Count of elements in first Array greater than second Array with each element considered only once
- Count distinct elements after adding each element of First Array with Second Array
- For each element in 1st array count elements less than or equal to it in 2nd array
- Count of elements to be inserted to make Array sum twice the XOR of Array
- For each element in 1st array count elements less than or equal to it in 2nd array | Set 2
- Count of elements A[i] such that A[i] + 1 is also present in the Array
- Count of elements which is the sum of a subarray of the given Array
- Count distinct elements in an array
- Count number of even and odd elements in an array
- Count elements present in first array but not in second

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.