-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcountPairsSum.cpp
More file actions
66 lines (66 loc) · 1.87 KB
/
Copy pathcountPairsSum.cpp
File metadata and controls
66 lines (66 loc) · 1.87 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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#include<iostream>
#include<vector>
#include<unordered_set>
#include<unordered_map>
#define mod 1000000007
long long helper(int no){
long long returnValue;
if(no%2==0){
int x = no/2;
returnValue = (((long long)x * (long long)(no+1)) % mod);
return returnValue;
}
else{
int x = (no+1)/2;
returnValue = (((long long)no * (long long)x) % mod);
return returnValue;
}
}
int solve(std::vector<int> &vec, int K){
long long returnValue = 0, lowPtr = 0, highPtr = vec.size()-1;
while(lowPtr<highPtr){
long long currSum = (long long)vec[lowPtr] + (long long)vec[highPtr];
if(currSum==(long long)K){
if(vec[lowPtr]==vec[highPtr]){
int no = highPtr-lowPtr;
returnValue = (returnValue + helper(no))%mod;
break;
}
else{
int freqLowPtr = 1,lowPtrValue = vec[lowPtr];
int freqHighPtr = 1,highPtrValue = vec[highPtr];
++lowPtr;
while(lowPtrValue==vec[lowPtr]){
++freqLowPtr;
++lowPtr;
}
--highPtr;
while(highPtrValue==vec[highPtr]){
++freqHighPtr;
--highPtr;
}
long long combinations = ((long long)freqLowPtr * (long long)freqHighPtr)%mod;
returnValue = (returnValue + combinations)%mod;
}
}
else if(currSum<(long long)K){
++lowPtr;
}
else{
--highPtr;
}
}
return returnValue;
}
int main(){
int N,K;
std::cin>>N;
std::vector<int> vec(N);
for(int index = 0;index<N;++index){
std::cin>>vec[index];
}
std::cin>>K;
int output = solve(vec,K);
//std::cout<<helper(7);
std::cout<<output<<std::endl;
}