human
Can you provide a code to bitwise operations on Subarrays of size K and the other steps required given the following:
Bitwise operations on Subarrays of size K | Function to convert bit array to decimal number.
Function to find maximum values of each bitwise OR operation on element of subarray of size K.
Maintain an integer array bit of size 32 all initialized to 0.
Create a sliding window of size k.
Function call.
Perform operation for removed element.
Perform operation for added_element.
Taking maximum value.
Return the result.
Driver Code.
Given array arr.
Given subarray size K.
Function call"
Solution in Python:
assistant
#Here is code to bitwise operations on Subarrays of size K and the other steps required.
 def build_num ( bit ) :
	ans = 0 ;
	for i in range ( 32 ) :
		if ( bit [ i ] > 0 ) :
			ans += ( 1 << i ) ;
	return ans ;
def maximumOR ( arr , n , k ) :
	bit = [ 0 ] * 32 ;
	for i in range ( k ) :
		for j in range ( 32 ) :
			if ( ( arr [ i ] & ( 1 << j ) ) > 0 ) :
				bit [ j ] += 1 ;
	max_or = build_num ( bit ) ;
	for i in range ( k , n ) :
		for j in range ( 32 ) :
			if ( ( arr [ i - k ] & ( 1 << j ) ) > 0 ) :
				bit [ j ] -= 1 ;
		for j in range ( 32 ) :
			if ( ( arr [ i ] & ( 1 << j ) ) > 0 ) :
				bit [ j ] += 1 ;
		max_or = max ( build_num ( bit ) , max_or ) ;
	return max_or ;
if __name__ == ' _ _ main _ _ ' :
	arr = [ 2 , 5 , 3 , 6 , 11 , 13 ] ;
	k = 3 ;
	n = len ( arr ) ;
	print ( maximumOR ( arr , n , k ) ) ;