Question:
Find the number of pairs of elements whose sum is equal to K using C++

Summary

Let's assume:

Array- arr[]

Integer- N, K

Function

getPairsCount()


Solution


//User function template for C++


class Solution{   

public:

    #include<bits/stdc++.h>

    int getPairsCount(int arr[], int n, int k) {

        unordered_map<int,int>mp;

        int ans=0;

        for(int i=0; i<n; i++){

            int y=k-arr[i];

            if(mp.count(y)>0){

                ans+=mp[y];

            }

            mp[arr[i]]++;

        }

        return ans;

        

    }

};


Answered  by:> >atulg8568

Credit: GeekforGeeks 


Suggested blogs:

>How to route between different components in React.js?

>How to save python yaml and load a nested class?-Python

>How to send multiple HTML form fields to PHP arrays via Ajax?

>What is data binding in Angular?

>What is microservice architecture, and why is it better than monolithic architecture?



Nisha Patel

Nisha Patel

Submit
0 Answers