Zig Zag Sequence

Sort by

recency

|

972 Discussions

|

  • + 0 comments

    Can't be done for C, Might as well share my solution:

    int compare(const void *a, const void b) { return ((const int *)a - *(const int *)b); }

    int main() { int cases; scanf("%d", &cases); for (int k = 0; k< cases; k++) { int n; scanf("%d", &n); int *arr = malloc(n * sizeof(int)); int *final = malloc(n * sizeof(int));
    for (int i = 0; i < n; i++) { scanf("%d", &arr[i]); }

        /* Enter your code here. Read input from STDIN. Print output to STDOUT */  
        qsort(arr, n, sizeof(int), compare);
        for (int i = 0; i < n/2; i++) {
            final[i] = arr[i];
            final[n - i- 1] = arr[n/2+i];
        }
        final[n/2] = arr[n-1];
        for (int i = 0; i < n; i++) {
            printf("%d ", final[i]);
        }
        printf("\n");
    }
    return 0;
    

    }

  • + 0 comments

    Too stiff check.
    In C++11 it allows the following set of changes:

    06: int mid = (n - 1) / 2;
    10: int ed = n - 2;
    14:   ed = ed - 1;
    

    but does allow the following:

    07: ; // dropped first swap
    09: int st = mid - 1;
    14:   ed = ed - 1;
    

    Though the resulting output is exactly the same.

  • + 0 comments

    This one in C# asks to create to whole class but it fails... Am I getting negatives cuz of performance?

    class Solution {
        static void Main(String[] args) {
            /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution */
            int queries = Convert.ToInt32(Console.ReadLine());
            int arrLength = Convert.ToInt32(Console.ReadLine());
            string[] arrStr = Console.ReadLine().Split(' ');
            int[] arr = new int[arrLength];
            int[] res = new int[arrLength];
    
            for (int count = 0; count < arrStr.Length; count++)
            {
                int item = Convert.ToInt32(arrStr[count]);
                arr[count] = item;
            }
    
            arr = arr.OrderBy(x => x).ToArray();
    
            for (int index = 0; index < arrLength; index++)
            {
                res[index] = arr[index < (arrLength - 1) / 2 ? index : (arrLength - index) + 2];
            }
    
            Console.WriteLine(String.Join(" ", res).Trim());
        }
    }
    
  • + 0 comments

    Typescript doesn't work, its missing the actual code...

  • + 0 comments

    The function isn't there when using R, just indicates where to write the code.