Zig Zag Sequence

Sort by

recency

|

154 Discussions

|

  • + 0 comments

    Python:

    def findZigZagSequence(a, n):
        a.sort()
        mid = int((n - 1)/2) #first
        a[mid], a[n-1] = a[n-1], a[mid]
    
        st = mid + 1
        ed = n - 2 #second
        while(st <= ed):
            a[st], a[ed] = a[ed], a[st]
            st = st + 1
            ed = ed - 1 #third
    
        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)
    
  • + 0 comments

    hackerrank should fix their stuff, there's no code in c#

  • + 0 comments

    am getting same output but it's shows wrong answer why ??

    include

    include

    include

    include

    int main() {

    /* Enter your code here. Read input from STDIN. Print output to STDOUT */    
    int temp, mid, l, n, k;
    scanf("%d",&l);
    scanf("%d",&n);
    int a[n];
    for(int i=0; i<n; i++){
        scanf("%d",&a[i]);
    }
    k=(n+1)/2;
    for(int i=0; i<n-1; i++){
        for(int j=i+1; j<n; j++)
        {
            if(a[i]>a[j]){
                temp=a[i];
                a[i]=a[j];
                a[j]=temp;
            }
        }
    }
    
    int temp1=a[k-1];
        a[k-1]=a[n-1];
        a[n-1]=temp1;
        int j=n-2;
    while(k<=j){
        int temp3=a[k];
        a[k]=a[j];
        a[j]=temp3;
        k++;
        j--;
    }
    
    
    for(int i=0;i<n; i++){
        printf("%d",a[i]);
        printf(" ");
    }
    
    return 0;
    

    }

  • + 0 comments

    The findZigZagSequence function is not provided.

  • + 1 comment

    The example provided in description seems misleading, for[2,3,1,5,4]the correct output is [1,2,5,4,3] , however in description we have [1,4,5,3,2] (altough it is mentioned it is a zigzag sequence).

    It would be better the descrption updated to say: the correct expected sequence in this case is [1,2,5,4,3]