OR subarrays problem

why the code is not working

#include
using namespace std;
int main () {
int n; cin>>n;
int p = (1<<n)-1;
int sum = 0;

int a[n];
for(int i = 1; i<=n;i++){
	cin>>a[i];
}
for(int i = 1; i<=p;i++){
	int x = i;
	int xr = 0;
	while(x>0){
		xr ^= a[x];
		x = x>>1;
	}
	sum += xr;

}
cout<<sum<<endl;
return 0;

}

your approach for solving this question is wrong. Please refer to this article for correct approach

NOW please check my approach

#include

using namespace std;

int main () {

int n; cin>>n;

int p = (1<<n)-1;

int sum = 0;

int a[n];

for(int i = 1; i<=n;i++){

cin>>a[i];

}

for(int i = 0; i<p;i++){

int x = i;

int xr = 0;

int ans = 0;

while(x>0){

  

  if(x&1){

    xr |= a[ans];

  }

  ans++;

  x = x>>1;

}

sum += xr;

}

cout<<sum<<endl;

return 0;

}

please share your code through ide.codingblocks.com

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.