in the example there are 6 numbers 1,-2,3,-4,-5,6
the negative numbers in the given list are -2,-4 and -5 where the -2 is replaced with corresponding last positive number encountered which is 1.similarly -4 and -5 are replaced with 3
so the output should be 1 1 3 3 3 6
sample input1
1 -2 3 -4 -5 6
sample output1
1 1 3 3 3 6
sample input2
1 11 -13 21 -19
sample output2
1 11 11 21 21
l = list(map(int, input().split()))
for i in range(len(l)):
if l[i] < 0:
l[i] = l[i-1]
print(*l)
Comments
Leave a comment