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++
// C++ implementation of the approach #include <bits/stdc++.h> using namespace std; const int N = 1000; // Function to return the count // of valid pairs int countPairs( int arr[], int n) { // Frequency array // Twice the original size to hold // negative elements as well int size = (2 * N) + 1; int freq[size] = { 0 }; // Update the frequency of each // of the array element for ( int i = 0; i < n; i++) { int x = arr[i]; // If say x = -1000 then we will place // the frequency of -1000 at // (-1000 + 1000 = 0) a[0] index freq[x + N]++; } // To store the count of valid pairs int ans = 0; // Remember we will check only for (even, even) // or (odd, odd) pairs of indexes as the average // of two consecutive elements is // a floating point number 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; } // Driver code int main() { int arr[] = { 4, 2, 5, 1, 3, 5 }; int n = sizeof (arr) / sizeof (arr[0]); cout << countPairs(arr, n); return 0; } |
Java
// Java implementation of the approach class GFG { static int N = 1000 ; // Function to return the count // of valid pairs static int countPairs( int arr[], int n) { // Frequency array // Twice the original size to hold // negative elements as well int size = ( 2 * N) + 1 ; int freq[] = new int [size]; // Update the frequency of each // of the array element for ( int i = 0 ; i < n; i++) { int x = arr[i]; // If say x = -1000 then we will place // the frequency of -1000 at // (-1000 + 1000 = 0) a[0] index freq[x + N]++; } // To store the count of valid pairs int ans = 0 ; // Remember we will check only for (even, even) // or (odd, odd) pairs of indexes as the average // of two consecutive elements is // a floating point number 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; } // Driver code public static void main(String[] args) { int arr[] = { 4 , 2 , 5 , 1 , 3 , 5 }; int n = arr.length; System.out.println(countPairs(arr, n)); } } // This code has been contributed by 29AjayKumar |
Python3
# Python 3 implementation of the approach N = 1000 # Function to return the count # of valid pairs def countPairs(arr, n): # Frequency array # Twice the original size to hold # negative elements as well size = ( 2 * N) + 1 freq = [ 0 for i in range (size)] # Update the frequency of each # of the array element for i in range (n): x = arr[i] # If say x = -1000 then we will place # the frequency of -1000 at # (-1000 + 1000 = 0) a[0] index freq[x + N] + = 1 # To store the count of valid pairs ans = 0 # Remember we will check only for (even, even) # or (odd, odd) pairs of indexes as the average # of two consecutive elements is # a floating point number 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 # Driver code if __name__ = = '__main__' : arr = [ 4 , 2 , 5 , 1 , 3 , 5 ] n = len (arr) print (countPairs(arr, n)) # This code is contributed by # Surendra_Gangwar |
C#
// C# implementation of the approach using System; class GFG { static int N = 1000; // Function to return the count // of valid pairs static int countPairs( int []arr, int n) { // Frequency array // Twice the original size to hold // negative elements as well int size = (2 * N) + 1; int []freq = new int [size]; // Update the frequency of each // of the array element for ( int i = 0; i < n; i++) { int x = arr[i]; // If say x = -1000 then we will place // the frequency of -1000 at // (-1000 + 1000 = 0) a[0] index freq[x + N]++; } // To store the count of valid pairs int ans = 0; // Remember we will check only for (even, even) // or (odd, odd) pairs of indexes as the average // of two consecutive elements is // a floating point number 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; } // Driver code public static void Main() { int []arr = {4, 2, 5, 1, 3, 5}; int n = arr.Length; Console.WriteLine(countPairs(arr, n)); } } // This code is contributed by AnkitRai01 |
Javascript
<script> // javascript implementation of the approach var N = 1000; // Function to return the count // of valid pairs function countPairs(arr , n) { // Frequency array // Twice the original size to hold // negative elements as well var size = (2 * N) + 1; var freq = Array(size).fill(0); // Update the frequency of each // of the array element for (i = 0; i < n; i++) { var x = arr[i]; // If say x = -1000 then we will place // the frequency of -1000 at // (-1000 + 1000 = 0) a[0] index freq[x + N]++; } // To store the count of valid pairs var ans = 0; // Remember we will check only for (even, even) // or (odd, odd) pairs of indexes as the average // of two consecutive elements is // a floating point number 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; } // Driver code var arr = [ 4, 2, 5, 1, 3, 5 ]; var n = arr.length; document.write(countPairs(arr, n)); // This code contributed by Rajput-Ji </script> |
7
Contact Us