-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmin_steps_array.py
More file actions
48 lines (35 loc) · 1.08 KB
/
min_steps_array.py
File metadata and controls
48 lines (35 loc) · 1.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
"""
Minimum steps to get desired array - GeeksForGeeks
Problem Link: https://practice.geeksforgeeks.org/problems/minimum-steps-to-get-desired-array/0
Author: Shyam Kumar
"""
def min_steps(arr, n):
count=0
while True:
# To check all elements are zero
all_zeros=True
# To check if all elements are even
even=True
for i,value in enumerate(arr):
if value != 0:
all_zeros=False
# If an element is off, decrease it and increase the count
if value%2 != 0: # Check for odd
arr[i]-=1
count+=1
even=False
# If all the elements are even
if even == True:
for i,value in enumerate(arr):
arr[i]=arr[i]//2
count+=1
# If all the elements are 0
if all_zeros == True:
break
return count-1
t=int(input())
for _ in range(t):
n=int(input())
arr=list(map(int, input().split()))
ans=min_steps(arr, n)
print(ans)