Zig Zag Sequence

  • + 1 comment
        /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */
    
        Scanner sc = new Scanner(System.in);
        int numTests = sc.nextInt();
    
        for (int i=0; i < numTests; i++) {
            int n = sc.nextInt();
            int k = (n+1)/2;
            List<Integer> list = new ArrayList<>();
            List<Integer> result = new ArrayList<>();
            for (int j=0; j < n; j++) {
                list.add(sc.nextInt());
            }
            sc.close();
    
            Collections.sort(list);
            result.addAll(list.subList(0, k-1));
            list.removeAll(list.subList(0, k-1));
            Collections.reverse(list);
            result.addAll(list);
    
            for (Integer val : result) {
                System.out.print(val + " ");
            }
            System.out.println();
        }