Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- It is a fact that for any such (i,j,k,l) ith + jth element must be equal to negative of kth +lth element.
- we are adding all i and j and put them into mp1 and vice versa for kth and lth elements.
- Now we are checking for total numbers of groups possible.
- */
- class Solution {
- public:
- int fourSumCount(vector<int>& nums1, vector<int>& nums2, vector<int>& nums3, vector<int>& nums4) {
- unordered_map<int,int> mp1,mp2;
- int n=nums1.size();
- for(int i=0;i<n;i++){
- for(int j=0;j<n;j++){
- mp1[nums1[i]+nums2[j]]++;
- }
- }
- for(int i=0;i<n;i++){
- for(int j=0;j<n;j++){
- mp2[nums3[i]+nums4[j]]++;
- }
- }
- int ans=0;
- for(auto itr:mp1){
- if(mp2[-(itr.first)]) ans+= itr.second*(mp2[-(itr.first)]);
- }
- return ans;
- }
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement