After spending much time the program runs correctly but shows timeout after validation. no pass, no fail just timeout. How can i resolve this? Thanks
package com.codegym.task.task07.task0720;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
/*
Shuffled just in time
*/
public class Solution {
public static void main(String[] args) throws IOException {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
int N = Integer.parseInt(reader.readLine());//read number from keyboard
int M = Integer.parseInt(reader.readLine());
ArrayList<String> strings = new ArrayList<String>(); //create a strings list
//write your code here
for(int i = 0; i < N; i++){//loop N number of times.
strings.add(reader.readLine()); //read string from keyboard
}
int counter = 0;
ArrayList<String> temp = new ArrayList<String>();//create a new array to add the first M strings
for(int i = 0; i < M; i++){//store the index of each of the first M number to the variable counter
counter = i;
// strings.add(strings.size(), strings.get(counter));//add the first M strings to the end of the list
temp.add(strings.get(counter));//add the first M string to the new array
}
for(int i = 0; i <temp.size(); i++){
counter = i;
strings.remove(temp.get(i));//remove the first M strings from the strings list
}
//System.out.println(temp.size());
for(int i = 0; i < temp.size(); i++) {
strings.add(temp.get(i));//add the first M string to the end of strings list
}
for(int i = 0; i < strings.size(); i++){
System.out.println(strings.get(i));//display the list each time on a new line
}
}
}