why i am getting WA in my code…can any1 tell me…
link to the above problem is
link to my answer is
why i am getting WA in my code…can any1 tell me…
link to the above problem is
link to my answer is
Can anyone check my code? I’m getting correct output, no idea why it says W/A.
It’s Python 3.5, I’m new user can’t ask question.
https://www.codechef.com/viewsolution/16331095
#greatest common divisor
def nwd(a,b):
if a == b:
return a
elif a % b == 0:
return a // b
else:
reszta = a % b
while reszta != 0:
wynik = b % reszta
if wynik == 0:
return reszta
break
b = reszta % wynik
if b == 0:
return wynik
break
reszta = wynik % b
if reszta == 0:
return b
break
#creating variables, arrays, deleting first number
t = int(input()) #tests
for i in range(t):
a = 0
tab, tab2, tab3 = [], [], []
tab = input().split(" ")
del tab[0]
#chaning input type to int
for j in range(len(tab)):
tab2.append(int(tab[j]))
#comparing
x = nwd(tab2[0], tab2[1])
tab3.append(tab2[0])
for j in range(0, len(tab2)-1):
tab3.append(nwd(tab3[0], tab2[j+1]))
del tab3[0]
#printing results
if min(tab3) == 1:
for j in range(len(tab2)):
print (tab2[j], end=" ")
print ("")
else:
maxx = max(tab3)
for j in range(len(tab2)):
print (tab2[j]//maxx, end=" ")
print ("")
Hi Ruhul!!
The problem with your code is that the variable mark stores the gcd of only the last two numbers of the array.
whereas it should store the gcd of all the numbers.
Let the array be 2,4,8,12. The variable mark stores gcd(8,12) =4. When the last loop runs, it checks a[1]%mark==0(2%4==0),this is not true so flag becomes 1 and displays the org array with no change. gcd of all the numbers is 2. mark should store 2. Then your answer will be 1,2,4,6.
what you can do is
initialise cool = gcd(a[1],a[2]); //gcd of first two numbers
then
for ( i=1 to n)
cool=gcd(cool, a[i+1]);
and then your code seems good.
All the best.
Thank you!!
understood…thank u… sorry for late reply to every1 who helped me…