Count pairs with average present in the same array
Given an array arr[] of N integers where |arr[i] ? 1000| for all valid i. The task is to count the pairs of integers from the array whose average is also present in the same array i.e. for (arr[i], arr[j]) to be a valid pair (arr[i] + arr[j]) / 2 must also be present in the array.
Examples:
Input: arr[] = {2, 1, 3}
Output: 1
Only valid pair is (1, 3) as (1 + 3) / 2 = 2 is also present in the array.
Input: arr[] = {4, 2, 5, 1, 3, 5}
Output: 7
Approach: Make a frequency array storing frequencies of every array element. Remember if the array contains negative numbers also then we have to take the size of the frequency array just double the original size. After updating the frequency array, there are two cases:
- If freq[i] > 0 then the total number of required pairs will be count = (freq[i] * (freq[i] – 1)) / 2.
- And for every pair (freq[i], freq[j]) where freq[i] > 0, freq[j] > 0 and freq[(i + j) / 2] > 0 then the total number of required pairs will be count = (freq[i] * freq[j]).
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
const int N = 1000;
int countPairs( int arr[], int n)
{
int size = (2 * N) + 1;
int freq[size] = { 0 };
for ( int i = 0; i < n; i++) {
int x = arr[i];
freq[x + N]++;
}
int ans = 0;
for ( int i = 0; i < size; i++) {
if (freq[i] > 0) {
ans += ((freq[i]) * (freq[i] - 1)) / 2;
for ( int j = i + 2; j < 2001; j += 2) {
if (freq[j] > 0 && (freq[(i + j) / 2] > 0)) {
ans += (freq[i] * freq[j]);
}
}
}
}
return ans;
}
int main()
{
int arr[] = { 4, 2, 5, 1, 3, 5 };
int n = sizeof (arr) / sizeof (arr[0]);
cout << countPairs(arr, n);
return 0;
}
|
Java
class GFG
{
static int N = 1000 ;
static int countPairs( int arr[], int n)
{
int size = ( 2 * N) + 1 ;
int freq[] = new int [size];
for ( int i = 0 ; i < n; i++)
{
int x = arr[i];
freq[x + N]++;
}
int ans = 0 ;
for ( int i = 0 ; i < size; i++)
{
if (freq[i] > 0 )
{
ans += ((freq[i]) * (freq[i] - 1 )) / 2 ;
for ( int j = i + 2 ; j < 2001 ; j += 2 )
{
if (freq[j] > 0 && (freq[(i + j) / 2 ] > 0 ))
{
ans += (freq[i] * freq[j]);
}
}
}
}
return ans;
}
public static void main(String[] args)
{
int arr[] = { 4 , 2 , 5 , 1 , 3 , 5 };
int n = arr.length;
System.out.println(countPairs(arr, n));
}
}
|
Python3
N = 1000
def countPairs(arr, n):
size = ( 2 * N) + 1
freq = [ 0 for i in range (size)]
for i in range (n):
x = arr[i]
freq[x + N] + = 1
ans = 0
for i in range (size):
if (freq[i] > 0 ):
ans + = int (((freq[i]) * (freq[i] - 1 )) / 2 )
for j in range (i + 2 , 2001 , 2 ):
if (freq[j] > 0 and
(freq[ int ((i + j) / 2 )] > 0 )):
ans + = (freq[i] * freq[j])
return ans
if __name__ = = '__main__' :
arr = [ 4 , 2 , 5 , 1 , 3 , 5 ]
n = len (arr)
print (countPairs(arr, n))
|
C#
using System;
class GFG
{
static int N = 1000;
static int countPairs( int []arr, int n)
{
int size = (2 * N) + 1;
int []freq = new int [size];
for ( int i = 0; i < n; i++)
{
int x = arr[i];
freq[x + N]++;
}
int ans = 0;
for ( int i = 0; i < size; i++)
{
if (freq[i] > 0)
{
ans += ((freq[i]) * (freq[i] - 1)) / 2;
for ( int j = i + 2; j < 2001; j += 2)
{
if (freq[j] > 0 && (freq[(i + j) / 2] > 0))
{
ans += (freq[i] * freq[j]);
}
}
}
}
return ans;
}
public static void Main()
{
int []arr = {4, 2, 5, 1, 3, 5};
int n = arr.Length;
Console.WriteLine(countPairs(arr, n));
}
}
|
Javascript
<script>
var N = 1000;
function countPairs(arr , n) {
var size = (2 * N) + 1;
var freq = Array(size).fill(0);
for (i = 0; i < n; i++) {
var x = arr[i];
freq[x + N]++;
}
var ans = 0;
for (i = 0; i < size; i++) {
if (freq[i] > 0) {
ans += ((freq[i]) * (freq[i] - 1)) / 2;
for (j = i + 2; j < 2001; j += 2) {
if (freq[j] > 0 && (freq[(i + j) / 2] > 0)) {
ans += (freq[i] * freq[j]);
}
}
}
}
return ans;
}
var arr = [ 4, 2, 5, 1, 3, 5 ];
var n = arr.length;
document.write(countPairs(arr, n));
</script>
|
Last Updated :
15 Nov, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...