Got WA despite following all conditions? (INCRDEC)

Please have a look at my code and tell what went wrong.

CodeChef: Practical coding for everyone

All i did was:
sort the given array, start copying to another array from i=0 while iterating i=i+2 at each step, and putting every ith element at the first and i+1th element at the back of the array

This would result in the max possible value of p, and also put a check that any time two sequential elements were same it would return a NO, since the elements had to be strictly +ve or -ve?

Now do the same with this test case
5
1 2 3 4 2

I got o/p as
YES
1 2 4 3 2

Is that not right, if not could you please elaborate as to how?

1 2 4 3 2 is correct

Shouldn’t it be 1 2 3 4 2 ?

both are correct
1 2 4 3 2 is increasing till 4 then it is decreasing

1 Like

hello i am new to codechef but can anyone tell when can we expect the editorial for this contest .

May I know why I got WA then? Is it possible that it could have been a memory issue?

(Tags: @admin3)

i think today itself (not sure)

can u explain ur code because it’s hard to understand :frowning:

Okay sure, im following below steps…
for eg… i/p is 1 4 2 3

  1. sort it in ascending order to get 1 2 3 4
  2. copy one by one… a[i] -> result_array[i] and a[i+1]-> result_array[last_index]
    this will give us a bell sort of our sorted array.
  3. Now to satisfy the other conditions (strictly increasing/decreasing) I placed a check every time i copy the elements that sees if the copied element matches its neighbors, if it does, it simply means o/p will be NO, a flag is set to 1, and I break out. or else i continue.
  4. in the end after the bell sort is done I simply check the flag and print YES and the result_array or NO

in eg case output will be

YES
1 3 4 2

I did this, please tell me where I did wrong
https://www.codechef.com/viewsolution/34819485

You forgot to attach the link to your solution

Now is it working

No you are not attaching it properly , you should share your submission page link of that question.
The link you attached is redirecting to submit a solution for that problem not your code.

import operator
try:
    for i in range(int(input())):
        n = int(input())
        a = list(map(int, input().split()))
        a.sort()
        b = set(a)
        b = list(b)
        if a == b:
            print("YES")
            print(*a)
        else:
            e = []
            c = {}
            for i in range(n):
                if a[i] in c:
                    c[a[i]] += 1
                    e.append(a[i])
                else:
                    c[a[i]] = 1
            #print(c)
            e.sort(reverse = True)
            #print(e)
            d = 0
            c = sorted(c.items(), key=operator.itemgetter(0))
            #c = dict(c)
            #print(c)
            if c[-1][1] > 1:
                print("NO")
            else:
                c = dict(c)
                for i in c:
                    if c[i] > 2:
                        d += 1
                if d > 0:
                    print("NO")
                else:
                    print("YES")
                    b = b + e
                    print(*b)
                    
except EOFError:
    pass
                

I don’t know, how to attach solution.

what did i do wrong with my solution.

check your output

@zgod
maybe youre not considering the case when the elements are similar at pos when the peak lies …
1
4
2 2 3 3
ans -> NO