Odd GCD codechef solution

 

Contest Code:START14B

 

Problem Code:BININVER

You are given an array A1,A2,,AN consisting of N integers. Your goal is to make the GCD of all the elements in the array an odd integer. To achieve this goal, you can do the following operation any number of times:

  • Choose an index i(1iN) such that Ai>1 and set Ai=Ai2 

You can choose an index multiple times during the operations. Find the minimum number of operations after which GCD of all the elements in the array becomes an odd integer.

Note: x : Returns the largest integer that is less than or equal to x (i.e rounds down to the nearest integer). For example, 1.5=1,2=2, 72  =3.5 =3.

Input Format

  • The first line of the input contains a single integer T denoting the number of test cases. The description of T test cases follows.
  • The first line of each test case contains a single integer N.
  • The second line contains N space-separated integers A1,A2,,AN.

Output Format

For each test case, print a single line containing one integer - the minimum number of operations after which GCD of all the elements in the array becomes an odd integer.

Constraints

  • 1T103
  • 1N105
  • 1Ai109
  • Sum of N over all test cases does not exceed 5105

Sample Input 1 

3
3
2 3 5
2
4 6
3 
4 12 24

Sample Output 1 

0
1 
2

Explanation

Test case 1: The GCD of all integers in the array is already 1, which is odd.

Test case 2: You choose the index i=2 and set A2=62 =3=3. So the array becomes [4,3] and the GCD of 4 and 3 is 

Solution on python 

def find(n):
    cnt = 0
    while n > 0 and n % 2 != 1:
        cnt += 1
        n //= 2
        
    return cnt
    
for _ in range(int(input())):
    n = int(input())
    l = list(map(int, input().split()))
    e = 0
    o = 0
    m = 1000000000
    for i in l:
        if i % 2 == 1: o += 1
        else:
            m = min(m, find(i))
            e += 1
    
    if o == 0: print(m)
    else: print(0)
def find(n):
cnt = 0
while n > 0 and n % 2 != 1:
cnt += 1
n //= 2
return cnt
for _ in range(int(input())):
n = int(input())
l = list(map(int, input().split()))
e = 0
o = 0
m = 1000000000
for i in l:
if i % 2 == 1: o += 1
else:
m = min(m, find(i))
e += 1
if o == 0: print(m)
else: print(0)

 

 

def find(n):
    cnt = 0
    while n > 0 and n % 2 != 1:
        cnt += 1
        n //= 2
        
    return cnt
    
for _ in range(int(input())):
    n = int(input())
    l = list(map(int, input().split()))
    e = 0
    o = 0
    m = 1000000000
    for i in l:
        if i % 2 == 1: o += 1
        else:
            m = min(m, find(i))
            e += 1
    
    if o == 0: print(m)
    else: print(0)

Comments

Popular Posts