For instance, if an array consists of elements {0,2,6,4,8,10} and the provided number is 8, then we must find pairs having sum of its elements as 8 i.e. {0,8} and {2,6}.
PROGRAM
// Program to find pairs in an array having sum equal to a given number
#include <iostream>
using namespace std;
void findPairs(int a[], int n, int sum)
{
int count=0;
for(int i=0 ; i<n ; i++)
{
for(int j=i+1 ; j<n ; j++)
{
if(a[i]+a[j]==sum)
{
count++;
cout<<"\nPAIR FOUND AT INDICES "<<i<<","<<j;
}
}
}
cout<<"\n\nTOTAL PAIRS HAVING SUM AS "<<sum<<" ARE "<<count;
}
int main()
{
int a[5],sum;
cout<<"ENTER ARRAY OF ELEMENTS: ";
for(int i=0 ; i<5 ; i++)
{
cin>>a[i];
}
cout<<"\nENTER SUM: ";
cin>>sum;
findPairs(a,5,sum);
}
OUTPUT