-
Notifications
You must be signed in to change notification settings - Fork 82
/
Count Good Meals.cpp
69 lines (61 loc) · 2.11 KB
/
Count Good Meals.cpp
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
67
68
69
/*
A good meal is a meal that contains exactly two different food items with a sum of deliciousness equal to a power of two.
You can pick any two different foods to make a good meal.
Given an array of integers deliciousness where deliciousness[i] is the deliciousness of the ith item of food,
return the number of different good meals you can make from this list modulo 10^9 + 7.
Note that items with different indices are considered different even if they have the same deliciousness value.
Input: deliciousness = [1,3,5,7,9]
Output: 4
Input: deliciousness = [1,1,1,3,3,3,7]
Output: 15
*/
#define N 1048576
#define M 1000000007
typedef long long int lli;
class Solution {
public:
int countPairs(vector<int>& nums) {
lli *arr=(lli *)calloc((N+1),sizeof(lli));
for(auto it=nums.begin();it!=nums.end();it++)
arr[*it]++;
lli pairs=0;
for(int i=0;i<=N;i++)
{
if(arr[i]>0)
{
for(int j=0;j<=21;j++)
{
lli val=pow(2,j);
lli temp=val-i;
if(temp<i||temp>N||arr[temp]==0)
continue;
if(temp==i)
{
if(arr[i]>1)
{
lli temp_ans=0;
lli val=arr[i]-1;
while(val>0)
{
temp_ans=((temp_ans%M)+(val%M))%M;
val--;
}
pairs=((pairs%M)+(temp_ans%M))%M;
}
else
{
continue;
}
}
else
{
lli temp_ans=((arr[i]%M)*(arr[temp]%M))%M;
pairs=((pairs%M)+(temp_ans%M))%M;
}
}
}
}
free(arr);
return pairs;
}
};