简体   繁体   中英

Array rotation TLE (Time Limit Exceeded)

I am really confused why my java code is not working it is giving TLE on Code Monks on Hacker Earth. Here is the link to the 1

import java.util.Scanner;
class TestClass {
    static int[] ar=new int[100001];
    public static void main(String args[] ){
        Scanner in=new Scanner(System.in);
        byte t=in.nextByte();
        while(t-->0){
            int n=in.nextInt();
            int k=in.nextInt()%n;
                for(int i=0;i<n-k;i++)
                    ar[i]=in.nextInt();
                for(int i=0;i<k;i++)
                    System.out.print(in.nextInt()+" ");
                for(int i=0;i<n-k;i++)
                    System.out.print(ar[i]+" ");
            System.out.println();
        }
    }
}

I don't know why is it giving TLE I think there is some infinite loop going.

the question at the site is-

Monk and Rotation Monk loves to perform different operations on arrays, and so being the principal of HackerEarth School, he assigned a task to his new student Mishki. Mishki will be provided with an integer array A of size N and an integer K , where she needs to rotate the array in the right direction by K steps and then print the resultant array. As she is new to the school, please help her to complete the task.

Input: The first line will consists of one integer T denoting the number of test cases. For each test case:

  1. The first line consists of two integers N and K, N being the number of elements in the array and K denotes the number of steps of rotation.
  2. The next line consists of N space separated integers , denoting the elements of the array A. Output: Print the required array.

Constraints:

1<=T<=20
1<=N<=10^5
0<=K<=10^6
0<=A[i]<=10^6

Sample Input

1

5 2

1 2 3 4 5

Sample Output

4 5 1 2 3

Explanation

Here T is 1, which means one test case.

denoting the number of elements in the array and , denoting the number of steps of rotations.

The initial array is: In first rotation, 5 will come in the first position and all other elements will move to one position ahead from their current position. Now, the resultant array will be

In second rotation, 4 will come in the first position and all other elements will move to one position ahead from their current position. Now, the resultant array will be

Time Limit: 1.0 sec(s) for each input file Memory Limit: 256 MB Source Limit: 1024 KB

I'm not sure about the correctness of your solution, but try to use StreamTokenizer or BufferedReader instead of Scanner. Scanner is too slow and may result in TLE when you need to read a lot of data.

Reduce the number of reads and writes from/to System.in and System.out. Look at the following solution

        Scanner scanner = new Scanner(System.in);
        int noOfTestCases = scanner.nextInt();

        for (int i = 0; i < noOfTestCases; i++) {
            int arraySize = scanner.nextInt();
            int noOfRotations = scanner.nextInt();
            noOfRotations = noOfRotations % arraySize;
            scanner.nextLine();
            String inputString = scanner.nextLine();
            String[] inputStringArray = inputString.split(" ");

            StringBuffer sb = new StringBuffer();

            for (int j = 0; j < arraySize; j++) {
                sb.append(inputStringArray[(arraySize + j - noOfRotations) % arraySize] + " ");
            }

            System.out.print(sb);
            System.out.println("");
        }
import java.util.*;
public class temp {
    public static void main (String[] args) {
        Scanner sc = new Scanner(System.in);
        int t = sc.nextInt();
        while(t-->0){
            int n = sc.nextInt();
            int k = sc.nextInt();
            int p = 0;
            int ar[] = new int[n];
            for(int i=0;i<n;i++){
                ar[i] = sc.nextInt();
            }
            k %= n;
            for(int i=0;i<n;i++){
                p = ar[(i+(n-k))%n];
                System.out.print(p+" ");
            }
            System.out.println();
        }       
    }
}

Though I have not used a big sized array in the starting, this code is working fine for all test cases.

Try this one.

Think from a different perspective. Instead of splitting the string and converting it into an array and applying the iterative logic, we can apply a different logic.

The trick is you just need to find the position of the input string where we have to split only once. By that I mean,
input=>
6 2 //4 is the length of numbers and 2 is the index of rotation
1 2 3 4 5 6 //array (take input as a string using buffered reader)

Here, we just need to split the array string at the 2nd last space ie 4th space. So the output can be achieved by just splitting the string once-
5 6 1 2 3 4
first split- 5 6 + space + second split- 1 2 3 4
This logic worked for me and all the test cases passed.

Also don't forget to cover the corner case scenario when array input string is just one number.

Code Snippet-

int count=0;
for(int k=0; k<arr.length(); k++) {
    if(arr.charAt(k)==' ')
        count++;
    if(count==size-rot) {
         System.out.println(arr.substring(k+1,arr.length())
             + " " + arr.substring(0,k));
         break;
    }
}

Problem is in the System.out.print() call that is inside the for-loop. Thats a fairly heavy call and if called too many times and creates an overhead. This solution works:

//import for Scanner and other utility classes
import java.util.*;

class TestClass {

    public static void main(String[] args) {
        Scanner s = new Scanner(System.in);
        String input = s.nextLine();
        int noOfTests = Integer.parseInt(input);
        for (int t = 0; t < noOfTests; t++) {
            input = s.nextLine();
            String[] str = input.split(" ");
            int sizeOfArray = Integer.parseInt(str[0]);
            int noOfRotations = Integer.parseInt(str[1]);
            String strIntegerArray = s.nextLine();
            String[] array = strIntegerArray.split(" ");
            printRightRotatedArray(array, noOfRotations, strIntegerArray.length());
        }
    }

    static void printRightRotatedArray(String[] array, int noOfRotations, int lengthOfStr) {
        int len = array.length;
        int noOfAcutalRotations = noOfRotations % len;
        int startingIndex = len - noOfAcutalRotations;
        StringBuilder sb = new StringBuilder(lengthOfStr+1);
        for (int i = 0; i < len; i++) {
            sb.append(array[(startingIndex + i) % len]);
            sb.append(" ");
        }
        System.out.println(sb);
    }
}

putting all into string buffer and print at the end worked for me.

class TestClass {
public static void main(String args[] ) throws Exception {
    //BufferedReader
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    int t = Integer.parseInt(br.readLine());
    StringBuilder sb = new StringBuilder();// output
    for(int i=0;i<t;i++){
        String [] nk=br.readLine().split(" ");
        int n= Integer.parseInt(nk[0]);
        int k=Integer.parseInt(nk[1]);
        String [] a=br.readLine().split(" ");
        int split=n-(k%n);
        for (int j = split; j < a.length; j++) {
            sb.append(a[j]);
            sb.append(' ');
        }
        for (int j = 0 ; j < split; j++) {
            sb.append(a[j]);
            sb.append(' ');
        }
        sb.append("\n");
    }
    System.out.println(sb);
}

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM