-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBitmask.cpp
More file actions
26 lines (24 loc) · 729 Bytes
/
Bitmask.cpp
File metadata and controls
26 lines (24 loc) · 729 Bytes
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
/// Author: Raiz
#include <bits/stdc++.h>
#define int long long
using namespace std;
/// Bitmasking is used to generate all possible subsets of a vector.
/// For each subset, we calculate its sum and keep track of the maximum.
/// This approach has O(2^n * n) complexity and works well for small n.
int Sum_Bitmask(const vector<int> &vec, const int &vec_size)
{
int Max_Sum = accumulate(vec.begin(), vec.end(), 0);
for(int mask = 0 ; mask < (1LL << vec_size) ; ++mask)
{
int Sum = 0;
for(int i = 0 ; i < vec_size ; ++i)
{
if(mask & (1LL << i))
{
Sum += vec[i];
}
}
Max_Sum = max(Max_Sum , Sum);
}
return Max_Sum;
}