How to get rid of Java TLE problem
Last Updated :
23 Mar, 2022
It happens many times that you have written correct Java code with as much optimization as needed according to the constraints. But, you get TLE ????.
This happens due to the time taken by Java to take input and write output using Scanner class which is slow as compared to BufferedReader and StringBuffer class. Read in detail about Scanner Class here.
Have a look at some tips to get rid of this TLE issue (when your logic is correct obviously)?
Tip 1 : Avoid using Scanner Class and try to use BufferedReader class.
Tip 2 : Try to use StringBuffer class in case you have to print large number of data.
Let’s take a problem from GeeksforGeeks practice and solve the TLE issue:
Problem : Segregate an Array of 0s, 1s and 2s
In short, problem is, given an array of 0s, 1s and 2s. We have to segregate all the 0s in starting of array, all the 1s in mid of the array, and all the 2s in last of the array.
Examples:
Input : 1 1 2 0 0 2 1
Output : 0 0 1 1 1 2 2
Approach : Segregate array of 0s, 1s and 2s
Below is the implementation of above Approach :
Java
import java.util.*;
import java.lang.*;
import java.io.*;
class GFG {
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
while (t-- > 0 ) {
int n = sc.nextInt();
int arr[] = new int [n];
for ( int i = 0 ; i < n; i++)
arr[i] = sc.nextInt();
segregateArr(arr, n);
for ( int i = 0 ; i < n; i++) {
System.out.print(arr[i] + " " );
}
System.out.println();
}
sc.close();
}
public static void segregateArr( int arr[], int n)
{
int low = 0 , high = n - 1 , mid = 0 ;
while (mid <= high) {
if (arr[mid] == 0 ) {
int temp = arr[low];
arr[low] = arr[mid];
arr[mid] = temp;
low++;
mid++;
}
else if (arr[mid] == 1 ) {
mid++;
}
else {
int temp = arr[mid];
arr[mid] = arr[high];
arr[high] = temp;
high--;
}
}
}
}
|
According to our expectations, it should pass all the testcases and get accepted on GeeksforGeeks practice. But, when we submit this code on GeeksforGeeks IDE, it shows TLE.
This signifies that we have exceeded the time limit as expected. Not an issue, let’s use the tips given above.
- Use BufferedReader to take input.
- Use StringBuffer to save and print output.
Approach : Segregate array of 0s, 1s and 2s
Below is the implementation of Java code for segregating 0s, 1s and 2s
Java
import java.io.*;
import java.util.*;
class GFG {
public static void main(String[] args) throws IOException
{
BufferedReader br = new BufferedReader( new InputStreamReader(System.in));
int t = Integer.parseInt(br.readLine());
while (t-- > 0 ) {
int n = Integer.parseInt(br.readLine());
int arr[] = new int [n];
String line = br.readLine();
String[] strs = line.trim().split( "\\s+" );
for ( int i = 0 ; i < n; i++)
arr[i] = Integer.parseInt(strs[i]);
segregateArr(arr, n);
StringBuffer sb = new StringBuffer();
for ( int i = 0 ; i < n; i++)
sb.append(arr[i] + " " );
System.out.println(sb);
}
br.close();
}
public static void segregateArr( int arr[], int n)
{
int low = 0 , high = n - 1 , mid = 0 ;
while (mid <= high) {
if (arr[mid] == 0 ) {
int temp = arr[low];
arr[low] = arr[mid];
arr[mid] = temp;
low++;
mid++;
}
else if (arr[mid] == 1 ) {
mid++;
}
else {
int temp = arr[mid];
arr[mid] = arr[high];
arr[high] = temp;
high--;
}
}
}
}
|
Great! You have leveled up.
Java TLE issue? Seems pretty much simple :). You may try it now.
Share your thoughts in the comments
Please Login to comment...