-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBitwiseOR_subArraySum.cpp
78 lines (63 loc) · 1.52 KB
/
BitwiseOR_subArraySum.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
70
71
72
73
74
75
76
77
78
// The integer array arr[] is given and print the resultant value of the bitwise OR
// of the sum of all the possible sub-arrays of the given array arr[].
// Note: A sub-array is a sequence of array-elements that could be drawn from other
// sequence of array-elements by removing zero or more elements without changing
// the order of the remaining elements.
// Input Format
// -------------
// Line-1: Read the size of array P.
// Line-2: Read the array elements(space separated values).
// Output Format
// --------------
// Print the resultant value.
// Sample input-1:
// ---------------
// 4
// 0 2 1 3
// Sample output-1:
// ----------------
// 7
// Explanation
// The sums of sub-arrays are
// 0 1 2 3 4 5 6.
// and bitwise OR operation
// 0 OR 1 OR 2 OR 3 OR 4 OR 5 OR 6 = 7.
// Sample input-2:
// ---------------
// 4
// 0 0 0 0
// Sample output-2:
// ----------------
// 0
// Explanation
// Only one sum sub-arrays i.e. 0.
#include<bits/stdc++.h>
using namespace std;
vector<int> res;
void solve(vector<int> &nums,int &sum,int ind){
if(ind==nums.size()){
res.push_back(sum);
return;
}
for(int i=ind;i<nums.size();i++){
sum=sum+nums[i];
solve(nums,sum,ind+1);
ind=ind+1;
sum=sum-nums[i];
}
}
int main(){
int n;
cin>>n;
vector<int> nums(n,0);
for(int i=0;i<nums.size();i++){
cin>>nums[i];
}
int sum=0;
solve(nums,sum,0);
int x=res[0];
for(int i=1;i<res.size();i++){
x=x|res[i];
}
cout<<x;
}