Count pairs from an array having equal sum and quotient

Given an array arr[] consisting of N integers, the task is to count the number of valid pairs (i, j) such that arr[i] + arr[j] = arr[i] / arr[j].
Examples:

Input: arr[] = {-4, -3, 0, 2, 1}Output: 1Explanation: The only possible pair is (0, 3) which satisfies the condition ( -4 + 2 = -4 / 2 (= -2) ).
Input: arr[] = {1, 2, 3, 4, 5}Output: 0

Naive Approach: The simple approach is to generate all possible pairs of the given array and count the number of pairs whose sum is equal to their division. After checking, all the pairs print the final count of possible pairs.Below is the implementation of the above approach:

C++14

#include
using namespace std;
  

int countPairs(int a[], int n)
{
    
    int count = 0;
  
    
    for (int i = 0; i < n; i++) {            for (int j = i + 1; j < n; j++) {                if (a[j] != 0                 && a[i] % a[j] == 0) {                                     if ((a[i] + a[j])                     == (a[i] / a[j]))                                             count++;             }         }     }             return count; }    int main() {     int arr[] = { -4, -3, 0, 2, 1 };     int N = sizeof(arr) / sizeof(arr[0]);     cout X + Y = X/Y=> X = Y 2/(1 – Y)

Follow the steps below to solve the above problem:
Initialize a variable, say count, to store the count of all possible pairs satisfying the required condition.
Initialize a Map to store the frequencies of values of the above expression obtained for each array element.
Traverse the given array using the variable i and perform the following steps:
If arr[i] is not equal to 1 and 0, then calculate arr[i] 2/(1 – arr[i]), say X.
Add the frequency of X in the Map to count.
Increase the frequency of arr[i] by 1 in the Map.

After completing the above steps, print the value of count as the result.
Below is the implementation of the above approach:

C++

#include
using namespace std;
  

int countPairs(int a[], int n)
{
    
    int count = 0;
  
    
    map mp;
  
    
    for (int i = 0; i < n; i++) {            int y = a[i];                     if (y != 0 && y != 1) {                             double x = ((y * 1.0)                         / (1 - y))                        * y;                                          count += mp[x];         }                     mp[y]++;     }             return count; }    int main() {     int arr[] = { -4, -3, 0, 2, 1 };     int N = sizeof(arr) / sizeof(arr[0]);             cout