-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path4sum II.cpp
More file actions
45 lines (37 loc) · 1.13 KB
/
4sum II.cpp
File metadata and controls
45 lines (37 loc) · 1.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
/*
Platform :- Leetcode
Problem :- 4Sum II
Approach :- find all pair sum of A,B using two for loop.
similarly find all pair sum of C,D and check id -ve of that sum is already present in A,B then increment it by number of times the -ve of sum is present
*/
class Solution {
public:
int fourSumCount(vector<int>& A, vector<int>& B, vector<int>& C, vector<int>& D) {
int c=0;int d=0;
map<int,int>P;
for(int i=0;i<A.size();++i){
for(int j=0;j<B.size();++j){
long int temp=A[i]+B[j];
if(temp){
P[temp]++;
}
else{
d++;
}
}
}
for(int i=0;i<C.size();++i){
for(int j=0;j<D.size();++j){
long int temp=C[i]+D[j];
temp*=-1;
if(temp){
c+=P[temp];
}
else{
c+=d;
}
}
}
return c;
}
};