Count of elements such that its sum/difference with X also exists in the Array
Last Updated :
02 Oct, 2023
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}
Brute Force Approach:
- Initialize a variable count to 0 to keep track of the count of elements that satisfy the condition.
- Loop through each element ‘num’ in the array arr.
- For each element ‘num’, check if either ‘num + x’ or ‘num – x’ is present in the array arr.
- If either ‘num + x’ or ‘num – x’ is present in the array arr, increment the count variable.
- After looping through all elements in the array arr, return the value of count.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void findAns( int arr[], int n, int x)
{
int count = 0;
for ( int i = 0; i < n; i++)
{
for ( int j = 0; j < n; j++)
{
if (i != j && (arr[i] - arr[j] == x || arr[j] - arr[i] == x))
{
count++;
break ;
}
}
}
cout << count << endl;
}
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
import java.util.*;
public class Main {
public static void findAns( int [] arr, int n, int x) {
int count = 0 ;
for ( int i = 0 ; i < n; i++) {
for ( int j = 0 ; j < n; j++) {
if (i != j && (arr[i] - arr[j] == x || arr[j] - arr[i] == x)) {
count++;
break ;
}
}
}
System.out.println(count);
}
public static void main(String[] args) {
int [] arr = { 2 , 2 , 4 , 5 , 6 };
int n = arr.length;
int x = 3 ;
findAns(arr, n, x);
}
}
|
Python3
def findAns(arr, n, x):
count = 0
for i in range (n):
for j in range (n):
if i ! = j and (arr[i] - arr[j] = = x or arr[j] - arr[i] = = x):
count + = 1
break
print (count)
if __name__ = = '__main__' :
arr = [ 2 , 2 , 4 , 5 , 6 ]
n = len (arr)
x = 3
findAns(arr, n, x)
|
C#
using System;
class Program
{
static void FindCount( int [] arr, int n, int x)
{
int count = 0;
for ( int i = 0; i < n; i++)
{
for ( int j = 0; j < n; j++)
{
if (i != j && (arr[i] - arr[j] == x || arr[j] - arr[i] == x))
{
count++;
break ;
}
}
}
Console.WriteLine(count);
}
static void Main()
{
int [] arr = { 2, 2, 4, 5, 6 };
int n = arr.Length;
int x = 3;
FindCount(arr, n, x);
}
}
|
Javascript
function findAns(arr, N, x) {
let count = 0;
for (let i = 0; i < N; i++) {
for (let j = 0; j < N; j++) {
if (i !== j && (arr[i] - arr[j] === x || arr[j] - arr[i] === x)) {
count++;
break ;
}
}
}
console.log(count);
}
const arr = [2, 2, 4, 5, 6];
const N = arr.length;
const x = 3;
GFG(arr, N, x);
|
Time Complexity: O(N^2)
Auxiliary Space: O(1)
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 implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void findAns( int arr[], int n, int x)
{
int ans;
unordered_set< int > s;
for ( int i = 0; i < n; i++)
s.insert(arr[i]);
ans = 0;
for ( int i = 0; i < n; i++) {
if (s.find(arr[i] + x) != s.end() || s.find(arr[i] - x) != s.end())
ans++;
}
cout << ans;
return ;
}
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
import java.util.*;
class GFG{
static void findAns( int arr[],
int n, int x)
{
int ans;
HashSet<Integer> s = new HashSet<Integer>();
for ( int i = 0 ; i < n; i++)
s.add(arr[i]);
ans = 0 ;
for ( int i = 0 ; i < n; i++)
{
if (s.contains(arr[i] + x) ||
s.contains(arr[i] - x))
ans++;
}
System.out.print(ans);
return ;
}
public static void main(String[] args)
{
int arr[] = { 2 , 2 , 4 , 5 , 6 };
int n = arr.length;
int x = 3 ;
findAns(arr, n, x);
}
}
|
Python3
def findAns(arr, n, x):
s = set ()
for i in range (n):
s.add(arr[i])
ans = 0
for i in range (n):
if arr[i] + x in s or arr[i] - x in s:
ans = ans + 1
print (ans)
arr = [ 2 , 2 , 4 , 5 , 6 ]
n = len (arr)
x = 3
findAns(arr, n, x)
|
C#
using System;
using System.Collections.Generic;
class GFG{
static void findAns( int [] arr,
int n, int x)
{
int ans;
HashSet< int > s = new HashSet< int >();
for ( int i = 0; i < n; i++)
s.Add(arr[i]);
ans = 0;
for ( int i = 0; i < n; i++)
{
if (s.Contains(arr[i] + x) ||
s.Contains(arr[i] - x))
ans++;
}
Console.Write(ans);
return ;
}
public static void Main(String[] args)
{
int [] arr = { 2, 2, 4, 5, 6 };
int n = arr.Length;
int x = 3;
findAns(arr, n, x);
}
}
|
Javascript
<script>
function findAns(arr, n, x)
{
let ans;
let s = new Set();
for (let i = 0; i < n; i++)
s.add(arr[i]);
ans = 0;
for (let i = 0; i < n; i++)
{
if (s.has(arr[i] + x) ||
s.has(arr[i] - x))
ans++;
}
document.write(ans);
return ;
}
let arr = [ 2, 2, 4, 5, 6 ];
let n = arr.length;
let x = 3;
findAns(arr, n, x);
</script>
|
Performance Analysis:
- Time Complexity: O(N)
- Auxiliary Space Complexity: O(N)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...