ANUUND - Editorial

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

can anyone help me out. Its working for all test cases still getting wrong answer.
2 approaches
http://www.codechef.com/viewsolution/6464869
http://www.codechef.com/viewsolution/6464697

In O(N) solution take A = {4,3,5,1,9}.
You will get wrong answer.

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?

1 Like

https://www.codechef.com/viewsolution/9114451
how can i remove the TLE? please help!

as you have said
“A now becomes = [1, 3, 4, 5].
Our array B will be [1, 5, 3, 4].”

but the solution of A = [1, 3, 4, 5] can also be [ 1 , 4 ,3 ,5] . This is also satisfying your condition. isn’t it ???

@Shashwat.delhi, The approach is already explained in the editorial :slight_smile:

@rhnvrm: Your solution is similar to O(N) solution explained in the editorial.

CodeChef: Practical coding for everyone . Plz tell me what is wrong with this soon. Thnx in advance

@invest123 You must take into account the separate test cases. you are printing for only one test case!

Thnx. OMG what a mistake :slight_smile: Dint notice that. I think i should use ideone to avoid these mistakes.

what is condition to test cases

He did not pass with this solution. I have updated the new solution.

Your solution is correct, but you are answering for a single test case only. You are not even reading number of test cases.

1 Like

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

1 Like

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.

your sorting step is O(N 2 ). You need faster algorithms for sorting. Eg quicksort, mergesort etc. Btw you can use simply STL function sort.

PLEASE CORRECT THE EDITORIAL TO
A[i] >= A[i + 1] if i is odd.
A[i] <= A[i + 1] if i is even.

Hello guys! I’m new to the platform.
How do I read the input using Pythong.
Thanks!

For a single integer you could use:

T = int(input())

And to get a set of whitespace-separated items into a list

items = input().split()

or if you want them as a list of integers:

values = list( map(int, input().split()) )
1 Like