Zig Zag Sequence

Sort by

recency

|

132 Discussions

|

  • + 0 comments

    in C++

    make three changes in the given code, to get sample test cases pass. modify your code to get debug exaclty...

    void findZigZagSequence(vector < int > a, int n){ sort(a.begin(), a.end()); int mid = (n + 1)/2-1; //1st change swap(a[mid], a[n-1]);

    int st = mid + 1;
    int ed = n - 2;              //2nd change
    while(st <= ed){
        swap(a[st], a[ed]);
        st = st + 1;
        ed = ed - 1;              //3rd change
    }
    for(int i = 0; i < n; i++){
        if(i > 0) cout << " ";
        cout << a[i];
    }
    cout << endl;
    

    }

  • + 0 comments

    In Python, I'm getting the same as the expected output but the tests fail.

    `def findZigZagSequence(a, n): a.sort() mid = (n - 1)//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 += 1
        ed -= 1
    
    for i in range(n):
        if i == n-1:
            print(a[i])
        else:
            print(a[i], end=' ')
    return
    
  • + 0 comments

    There is no code provided in JavaScript

  • + 0 comments

    There doesn't seem to be a way to do this in typescript as @types/node is not included (is there a way to import modules with the hackerrank platform?). This error will appear when you try to run. solution.ts(3,1): error TS2580: Cannot find name 'process'. Do you need to install type definitions for node? Try npm i @types/node.

  • + 0 comments

    JAVA:

    public static void findZigZagSequence(int [] a, int n){ Arrays.sort(a); int mid = n/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();
    }