1

I wrote a program in C# and I am getting Time Limit Exceed in last 3 test Cases. What is wrong with my code? Need Help! I getting error when there is more number of- Test Case- 20 No of Array Elements and No of Rotations- 9384 208886

static void ShowResult(int[] A,int k,int n)
    {
        int rcount = 1;
        while (rcount <= k)
        {
            int temp = A[n - 1];
            for (int i = n - 1; i != 0; i--)
            {
                A[i] = A[i - 1];
            }
            A[0] = temp;
            rcount++;
        }
        foreach (var res in A)
        {
            Console.Write(res+" ");
        }
        Console.WriteLine();
    }
static void Main(String[] args)
    {
        int N;
        int K;
        int flag=1;
        int T=Convert.ToInt32(Console.ReadLine());
            while(flag<=T)
                {
                string str1=Console.ReadLine();
                string[] strarr1=new string[2];
                strarr1=str1.Split(' ',(char)2);
                N=Convert.ToInt32(strarr1[0]);
                K=Convert.ToInt32(strarr1[1]);
                string str2=Console.ReadLine();
                int[] A=Array.ConvertAll(str2.Split(' '),int.Parse);
                ShowResult(A, K, N);
                flag++;
                }

[enter image description here][1]

Sample Input

1

5 2

1 2 3 4 5

Sample Output

4 5 1 2 3

Explanation

Here 1 is, no of test case. Here 5 and 2 are no of array is 5 and no of rotation is 2. For first rotation: 51234 For 2nd Rotation: 45123 So output will be 45123

  • Isn't it a coding challenge? – Arcord Feb 24 '21 at 12:16
  • No, its a practice problem from code monk, above program passes 3 test case but it has time limit issue if test cases are more than 20. – Prince Roshan Feb 24 '21 at 12:20
  • 1
    Do you have a proper question? May you [edit] your question to match [ask], and [mre] without fluff. pinpointing your issue. – Drag and Drop Feb 24 '21 at 12:20
  • 1
    Simple question ? If the number of rotation is big, could it be simplify using the array size? – Drag and Drop Feb 24 '21 at 12:26
  • Yes, something like that. – Prince Roshan Feb 24 '21 at 12:28
  • 1
    it's call Modulo the `%` operator https://learn.microsoft.com/en-us/previous-versions/visualstudio/visual-studio-2010/0w4e0fzs(v=vs.100) – Drag and Drop Feb 24 '21 at 12:29
  • Performance wise there is many way to improve the code here. 1. If I wrote an array on paper. And give you a number of rotation, a scissor, and tape. you will simply make one cut in this array and glue it the the other way. You can do that with array copy. 2. Given the array 123, and 2 rotation will you rotate left once or right twice ? – Drag and Drop Feb 24 '21 at 12:41
  • The easies way will be no not do anything and simply store an index I indicating the start of the array. This will be a simple class inheriting from array. and one additional property. Other write `this[]` with modlo operator and the array will never need to be be moved. Only the starting index will, and it's a no cost int. – Drag and Drop Feb 24 '21 at 12:46
  • Yes, it works but comes to single test case time limit exceed – Prince Roshan Feb 24 '21 at 16:35

0 Answers0