Find the number of elements X such that X + K also exists in the array
Last Updated :
03 Jul, 2023
Given an array a[] and an integer k, find the number of elements x in this array such that the sum of x and k is also present in the array.
Examples:
Input: { 3, 6, 2, 8, 7, 6, 5, 9 } and k = 2
Output: 5
Explanation:
Elements {3, 6, 7, 6, 5} in this array have x + 2 value that is
{5, 8, 9, 8, 7} present in this array.
Input: { 1, 2, 3, 4, 5} and k = 1
Output: 4
Explanation:
Elements {1, 2, 3, 4} in this array have x + 2 value that is
{2, 3, 4, 5} present in this array.
The problem is similar to finding the count of pairs with a given sum in an Array.
Brute Force Approach:
- Initialize a counter variable to 0 to keep track of the number of elements that satisfy the given condition.
- Use nested loops to iterate over all possible pairs of elements in the array.
- For each pair, check if their sum is equal to k plus any element in the array. If it is, increment the counter by 1.
- After all pairs have been checked, return the counter variable as the output.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int count_element( int N, int K, int * arr)
{
int count = 0;
for ( int i=0; i<N; i++){
for ( int j=0; j<N; j++){
if (arr[j] == arr[i] + K){
count++;
break ;
}
}
}
return count;
}
int main()
{
int arr[] = { 3, 6, 2, 8, 7, 6, 5, 9 };
int N = sizeof (arr) / sizeof (arr[0]);
int K = 2;
cout << count_element(N, K, arr);
return 0;
}
|
Java
import java.util.Arrays;
public class Main {
public static int count_element( int N, int K, int [] arr) {
int count = 0 ;
for ( int i = 0 ; i < N; i++) {
for ( int j = 0 ; j < N; j++) {
if (arr[j] == arr[i] + K) {
count++;
break ;
}
}
}
return count;
}
public static void main(String[] args) {
int [] arr = { 3 , 6 , 2 , 8 , 7 , 6 , 5 , 9 };
int N = arr.length;
int K = 2 ;
System.out.println(count_element(N, K, arr));
}
}
|
Python3
arr = [ 3 , 6 , 2 , 8 , 7 , 6 , 5 , 9 ]
def count_element(N: int , K: int , arr: list ) - > int :
count = 0
for i in range (N):
for j in range (N):
if arr[j] = = arr[i] + K:
count + = 1
break
return count
N = len (arr)
K = 2
print (count_element(N, K, arr))
|
C#
using System;
class GFG
{
public static int count_element( int N, int K, int [] arr)
{
int count = 0;
for ( int i = 0; i < N; i++)
{
for ( int j = 0; j < N; j++)
{
if (arr[j] == arr[i] + K)
{
count++;
break ;
}
}
}
return count;
}
public static void Main( string [] args)
{
int [] arr = { 3, 6, 2, 8, 7, 6, 5, 9 };
int N = arr.Length;
int K = 2;
Console.WriteLine(count_element(N, K, arr));
}
}
|
Javascript
function count_element(N, K, arr) {
let count = 0;
for (let i=0; i<N; i++){
for (let j=0; j<N; j++){
if (arr[j] == arr[i] + K){
count++;
break ;
}
}
}
return count;
}
const arr = [3, 6, 2, 8, 7, 6, 5, 9];
const N = arr.length;
const K = 2;
console.log(count_element(N, K, arr));
|
Time Complexity: O(N^2)
Auxiliary Space: O(1)
Efficient Approach:
The efficient approach to solve the problem is to use a HashMap, the key in map will act as the unique element in this array and the corresponding value will tell us the frequency of this element.
Iterate over this map and find for each key x in this map whether the key x + k exists in the map or not, if exist then add this frequency to the answer i.e for this element x we have x+k in this array. Finally, return the answer.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int count_element( int N, int K, int * arr)
{
map< int , int > mp;
for ( int i = 0; i < N; ++i)
mp[arr[i]]++;
int answer = 0;
for ( auto i : mp) {
if (mp.find(i.first + K) != mp.end())
answer += i.second;
}
return answer;
}
int main()
{
int arr[] = { 3, 6, 2, 8, 7, 6, 5, 9 };
int N = sizeof (arr) / sizeof (arr[0]);
int K = 2;
cout << count_element(N, K, arr);
return 0;
}
|
Java
import java.util.*;
class GFG{
static int count_element( int N, int K, int [] arr)
{
HashMap<Integer,Integer> mp = new HashMap<Integer,Integer>();
for ( int i = 0 ; i < N; ++i)
if (mp.containsKey(arr[i])){
mp.put(arr[i], mp.get(arr[i])+ 1 );
} else {
mp.put(arr[i], 1 );
}
int answer = 0 ;
for (Map.Entry<Integer,Integer> i : mp.entrySet()) {
if (mp.containsKey(i.getKey() + K) )
answer += i.getValue();
}
return answer;
}
public static void main(String[] args)
{
int arr[] = { 3 , 6 , 2 , 8 , 7 , 6 , 5 , 9 };
int N = arr.length;
int K = 2 ;
System.out.print(count_element(N, K, arr));
}
}
|
Python3
def count_element(N, K, arr):
mp = dict ()
for i in range (N):
mp[arr[i]] = mp.get(arr[i], 0 ) + 1
answer = 0
for i in mp:
if i + K in mp:
answer + = mp[i]
return answer
if __name__ = = '__main__' :
arr = [ 3 , 6 , 2 , 8 , 7 , 6 , 5 , 9 ]
N = len (arr)
K = 2
print (count_element(N, K, arr))
|
C#
using System;
using System.Collections.Generic;
public class GFG{
static int count_element( int N, int K, int [] arr)
{
Dictionary< int , int > mp = new Dictionary< int , int >();
for ( int i = 0; i < N; ++i)
if (mp.ContainsKey(arr[i])){
mp[arr[i]] = mp[arr[i]]+1;
} else {
mp.Add(arr[i], 1);
}
int answer = 0;
foreach (KeyValuePair< int , int > i in mp) {
if (mp.ContainsKey(i.Key + K) )
answer += i.Value;
}
return answer;
}
public static void Main(String[] args)
{
int []arr = { 3, 6, 2, 8, 7, 6, 5, 9 };
int N = arr.Length;
int K = 2;
Console.Write(count_element(N, K, arr));
}
}
|
Javascript
<script>
function count_element(N, K, arr)
{
let mp = new Map();
for (let i = 0; i < N; ++i)
if (mp.has(arr[i])) {
mp.set(arr[i], mp.get(arr[i]) + 1)
} else {
mp.set(arr[i], 1)
}
let answer = 0;
for (let i of mp) {
if (mp.has(i[0] + K))
answer += i[1];
}
return answer;
}
let arr = [3, 6, 2, 8, 7, 6, 5, 9];
let N = arr.length;
let K = 2;
document.write(count_element(N, K, arr));
</script>
|
Time Complexity: O(N log N), where N is the size of the input array
Space Complexity: O(N)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...