human
What is a code to smallest power of 2 which is greater than or equal to sum of array elements and the other steps required given the following:
Smallest power of 2 which is greater than or equal to sum of array elements | Function to find the nearest power of 2.
The number.
If already a power of 2.
Find the next power of 2.
Function to find the memory used.
Sum of array.
Traverse and find the sum of array.
Function call to find the nearest power of 2.
Driver Code"
How can the above be solved in Python?
assistant
#Here is code to smallest power of 2 which is greater than or equal to sum of array elements and the other steps required.
 def nextPowerOf2 ( n ) :
	p = 1
	if ( n and not ( n & ( n - 1 ) ) ) :
		return n
	while ( p < n ) :
		p <<= 1
	return p
def memoryUsed ( arr , n ) :
	sum = 0
	for i in range ( n ) :
		sum += arr [ i ]
	nearest = nextPowerOf2 ( sum )
	return nearest
arr = [ 1 , 2 , 3 , 2 ]
n = len ( arr )
print ( memoryUsed ( arr , n ) )