DEV Community

Carnato
Carnato

Posted on

Count Pair With given Sum using map

Solution

#include<bits/stdc++.h>
using namespace std;
void fun(int arr[],int n,int k)
{

}
int main()
{
   int arr[]={1,2,5,4,0};
   int n=5;
   int k=5;
    unordered_map<int,int>m;
        int c=0;

        for(int i=0;i<n;i++)
        {
            c+=m[k-arr[i]];

            m[arr[i]]++;
        }
        cout<<c;
    return 0;
}
Enter fullscreen mode Exit fullscreen mode

Discussion (0)