Computer Science, asked by ashrithaboyina47, 7 months ago

given an array of integers, perform atmost K operations so that the sum of elements of final array is minimum. An operation is defined as follows - Consider any 1 element from the array, arr[i]. Replace arr[i] by floor(arr[i]/2). Perform next operations on updated array. The task is to minimize the sum after atmost K operations. Constraints 1 Select 20. Replace it by 10. New array = [10, 7, 5, 4] Operation 2 -> Select 10. Replace it by 5. New array = [5, 7, 5, 4]. Operation 3 -> Select 7. Replace it by 3. New array = [5,3,5,4]. Sum = 17.

Answers

Answered by sharathicwai
4

Answer:

a,b = input().strip().split(" ")

arr = list(map(int,input().strip().split(" ")))

b = int(b)

a = len(arr)

while b:

   b=b-1

   c=(max(arr)//2)

   arr.remove(max(arr))

   arr.append(c)

print(sum(arr))

Explanation:

It will give you a presentation error, but dont worry, submit it

Answered by Harishsantosh
0

Answer:

n,k = input().strip().split(" ")

values = list(map(int,input().strip().split(" ")))

k= int(k)

for i in range(k,0,-1):

   temp = (max(values)//2)

   values.remove(max(values))

   values.append(temp)

print(sum(values),end="")            

Explanation.

it will accept the code but you will get time limit error you will not get any presentation error. if any any one knows how to reduce time complexity please let me know

Similar questions