Click here to Skip to main content
15,889,808 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Given an array consisting of N positive integers, find the sum of bit-wise and of all possible sub-arrays of the array.

What I have tried:

I tried using 3 loops and getting sum of AND products of all subarrays. But I want an efficient solution.

Below solution I don't understand the use of 30 in line 19 of c++ program. May be it will work only for 30 elements?
Please help.

Sum of bitwise AND of all subarrays - GeeksforGeeks
Posted
Updated 26-Feb-20 22:14pm
v3
Comments
John R. Shaw 27-Feb-20 5:12am    
If this was not an academic (home work) question, I would tell the person asking the question to reword it or jump off the nearest bridge. Researching other peoples solutions is not the same as finding your own. You need to break down the question piece by piece and figure it out from there. Other peoples solutions are not the only solution; who knows your untainted solution may be better.

Quote:
Below solution I don't understand the use of 30 in line 19 of c++ program.

reread this part:
Quote: Sum of bitwise AND of all subarrays - GeeksforGeeks
Efficient Solution: For the sake of better understanding, let’s assume that any bit of an element is represented by the variable ‘i’ and the variable ‘sum’ is used to store the final sum.

The code is assuming int are 32 bits and numbers are never negative.
So 31 bits to check, from bit 0 to 30.
So the program fail with negative numbers.
 
Share this answer
 
So go back to the original article and click on the feedback button at the bottom - the original author will understand his code and should be able to give you a better explanation of how it works than a random site that hasn't seen it before.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900