I don’t know why am i getting wrong answer for my code …seems to work fine…any kind of help will be appreciated …thanks
t=int(input())
while t>0:
size=int(input())
array=[int(i) for i in input().split()]
array2=array
array2.sort()
i=1
for i in range(i,len(array),2):
if i==len(array2)or i==len(array2)-1:
break
else:
j=i+1
a=array2[i]
array2[i]=array2[j]
array2[j]=a
print(array2)
t-=1
My Answer :
Sort the array.
print the first element , n den swap the next two
So, if input is,
5 4 3 2 1
it will sort: 1 2 3 4 5
n output will b like
1 3 2 5 4
Whats wrong in this?
I am not sure what the error was but the error was probably in map(int,s.split(" ")) , when I replaced it with [int(x) for x in s.split()] it worked .
Here is your corrected code . http://www.codechef.com/viewsolution/3925500
You are declaring the array in while (testcase --) loop, which is not correct. Number of test cases could be very huge, If you keep allocating so much memory for so many loops, you will get TLE. Ideally you should use vector or you should declare the array globally.