Zig Zag Sequence

Sort by

recency

|

155 Discussions

|

  • + 0 comments

    with C#: Output as expected but somwehow not passing not even the first test case.

    Error

    Traceback (most recent call last):
      File "Solution.py", line 178, in <module>
        run_custom_checker(t_obj, r_obj)
      File "Solution.py", line 77, in run_custom_checker
        system_code = code_data[t_obj.submission_language].strip()
    KeyError: u'csharp'Traceback (most recent call last):
      File "Solution.py", line 178, in <module>
        run_custom_checker(t_obj, r_obj)
      File "Solution.py", line 77, in run_custom_checker
        system_code = code_data[t_obj.submission_language].strip()
    KeyError: u'csharp'
    

    **C# code **

    using System;
    using System.Collections.Generic;
    using System.IO;
    
    
    class Solution
    {
    
        private static List<int> findZigZagSequence(List<int> ar)
        {
            List<int> zigZagedList = ar.ToList();
            zigZagedList.Sort();
            zigZagedList[ar.Count / 2] = (from n in ar select n).Max();
            zigZagedList[ar.Count - 1] = ar[ar.Count / 2];
            int startIndex = (ar.Count / 2) + 1;
            int endIndex = ar.Count - startIndex;
            List<int> decreasingList = zigZagedList.GetRange(startIndex, endIndex);
            decreasingList.Sort((a, b) => b.CompareTo(a));
            zigZagedList.RemoveRange(startIndex, endIndex);
            zigZagedList.AddRange(decreasingList);
    
            return zigZagedList;
        }
    
        static void Main(String[] args)
        {
            int t = Convert.ToInt32(Console.ReadLine().Trim());
    
    
            for (int i = 0; i < t; i++)
            {
                int n = Convert.ToInt32(Console.ReadLine().Trim());
                List<int> ar = Console.ReadLine().TrimEnd().Split(' ').ToList().Select(arTemp => Convert.ToInt32(arTemp)).ToList();
                Console.WriteLine(String.Join(' ', findZigZagSequence(ar)));
            }
        }
    }
    }
    
  • + 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.