Zig Zag Sequence

Sort by

recency

|

1046 Discussions

|

  • + 1 comment

    I got my output equal to the expected and I only modified 3 lines of code. Still getting the test failing. Using Java 8.

        public static void findZigZagSequence(int [] a, int n){
            Arrays.sort(a);
            int mid = (n + 1)/2;
            int temp = a[--mid];
            a[mid] = a[n - 1];
            a[n - 1] = temp;
        
            int st = mid + 1;
            int ed = n - 2;
            while(st <= ed){
                temp = a[st];
                a[st] = a[ed];
                a[ed] = temp;
                st = st + 1;
                ed = ed - 1;
            }
            for(int i = 0; i < n; i++){
                if(i > 0) System.out.print(" ");
                System.out.print(a[i]);
            }
            System.out.println();
        }
    
  • + 1 comment

    i have got the same expected result as the output, still saying u failed. i just dont why ?

    • def findZigZagSequence(a, n):
    • a.sort()
    • mid = int((n + 1)//2)-1
    • a[mid], a[n-1] = a[n-1], a[mid]
    • c=a[:mid+1]
    • b=sorted(a[mid+1:],reverse=True)
    • a=c+b
    • print(" ".join(map(str, a)))
      • test_cases = int(input())
    • "
    • for cs in range (test_cases):
    • n = int(input())
    • a = list(map(int, input().split()))
    • findZigZagSequence(a, n)
  • + 1 comment
    def findZigZagSequence(a, n):
        a.sort()
        mid = n//2
        a[mid], a[n-1] = a[n-1], a[mid]
    
        st = mid + 1
        ed = n - 2
        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)
    `
    
    This python solutions is Success. but I feel it's wrong answer.(line 3:` mid // 2`  Doesn't meet prerequisites)
    
  • + 0 comments

    With Java 8 it works for me, with Java 7 it doesn't. It's better to refresh and make only the 3 required line changes.

  • + 0 comments

    Submitted a support ticket for this specific challenge. Since as others manifested here. The code to modify is not present in all the languages just in a few. Hope it helps for new people checking the challenge.