Getting the wrong answer even though the expected output = output (HackerRank)

356 Views Asked by At

I'm on HackerRank doing the zigzag sequence and although my output is the same as the expected output, it keeps saying I got the wrong answer. Had the same problem with my mock test earlier and am extremely frustrated by this since I couldn't finish the test on time because of it. Has anyone experienced anything similar on HackerRank?

my output:1 2 3 7 6 5 4 expected output: 1 2 3 7 6 5 4

My code is written below:

def findZigZagSequence(a, n):

    a.sort()
    mid = int((n + 1)/2) - 1
    a[mid], a[n-1] = a[n-1], a[mid]

    st = mid + 1 # 4ele = 5
    ed = n - 2 #5ele = 6
    while(st <= ed):
        a[st], a[ed] = a[ed], a[st]
        st = st + 1
        ed = ed - 1

    for i in range (n):
        if i == n-1:
            print(a[i])
        else:
            print(a[i], end = ' ')
    return

test_cases = int(input())
for cs in range (test_cases):
    n = int(input())
    a = list(map(int, input().split()))
    findZigZagSequence(a, n)
1

There are 1 best solutions below

2
Arjit On

The tricky thing with some hackerrank solutions is that they are dependent on what the author expects to see.

For this case, the author wants us to just modify 3 lines. See my comments below.

def findZigZagSequence(a, n):
    a.sort()
    mid = int((n - 1)/2)             #line1
    a[mid], a[n-1] = a[n-1], a[mid]

    st = mid + 1
    ed = n - 2                       #line2
    while(st <= ed):
        a[st], a[ed] = a[ed], a[st]
        st = st + 1
        ed = ed - 1                  #line3

    for i in range (n):
        if i == n-1:
            print(a[i])
        else:
            print(a[i], end = ' ')
    return

test_cases = int(input())
for cs in range (test_cases):
    n = int(input())
    a = list(map(int, input().split()))
    findZigZagSequence(a, n)