Question:
Find the Count pairs with a given sum
For Example:
Array [8, 5, 7, 2, 5]
Sum=10
The Count of pairs =2
#include<iostream>
using namespace std;
int getCount(int arr[], int n, int sum)
{
int count = 0;
for (int i = 0; i < n; i++)
for (int j = i + 1; j < n; j++)
if (arr[i] + arr[j] == sum)
count++;
return count;
}
int main()
{
int arr[] = { 8, 5, 7, 2, 5 };
int n = sizeof(arr) / sizeof(arr[0]);
int sum = 10;
cout << "The Count of pairs is "
<< getCount(arr, n, sum);
return 0;
}
---------End---------