How to Avoid TLE While Coding in Java?
Last Updated :
29 Mar, 2023
Avoiding Time Limit Exceeded(TLE) is one of the most important aspects that we need to take care of while doing competitive coding as well as while answering questions on DSA during a technical PI. In this context, one commonly asked question across all coding platforms is sorting or problems that involve sorting. The following code is something I have devised while practicing Data Structures, and it linearly sorts a list of consecutive numbers starting from 1 in a minimum number of swaps and returns the value of the number of swaps done.
The concept behind the following code is that the correct position of a number in a sorted array is one less than the number itself, considering that the array index starts from 0. This is because the array only consists of consecutive numbers starting from one.
Illustration: Consider the following example: The given unsorted array is: 4 3 1 2 5.
Input: Below is a table of the array elements with their current position in the array (indicated by the indices in the first row).
Output: After sorting, the array is going to look as shown below. Thus, the correct position of the elements is nothing but an index of value one less than the number itself and hence all we need to do is put the respective elements at their correct indices which can be determined from the element value.
Approach:
- We start by inserting the given array elements into a HashMap where the key is the element and the value is the index indicating the current position.
- Then we run a for loop starting from the initial index value of the input array (i.e, 0) up-to a value one less than the size of the array.
- In every iteration, we check if the value at arr[i] is one more than ‘i’ or not.
- If so, it means that the element is at its right position, or else its position needs to be swapped with that of the element whose position it actually is (that element is nothing but (i+1)). Every time a swapping happens the variable meant for keeping count of the number of swaps is incremented by one.
- Then corresponding changes in the array due to swapping are updated in the HashMap as well.
Pictorial representation: Initially, the HashMap is as follows
Element(Key) |
Current Index(Value) |
4
|
0
|
3
|
1
|
1
|
2
|
2
|
3
|
5
|
4
|
After the first iteration, the HashMap will look like this
Element(Key) |
Current Index(Value) |
4
|
2
|
3
|
1
|
1
|
0
|
2
|
3
|
5
|
4
|
While the array becomes
Conclusion: Thus, after every swap one element gets placed at its correct position, and corresponding changes made to the hashmap ensure that the number of swaps is minimum.
Implementation:
Java
import java.io.*;
import java.math.*;
import java.security.*;
import java.text.*;
import java.util.*;
import java.util.concurrent.*;
import java.util.regex.*;
class GFG {
static int minimumSwaps( int [] arr)
{
int swap = 0 , index = 0 ;
HashMap<Integer, Integer> index_table
= new HashMap<>();
for ( int i = 0 ; i < arr.length; i++) {
index_table.put(arr[i], i);
}
for ( int i = 0 ; i < arr.length; i++) {
if (arr[i] != (i + 1 ))
{
index = index_table.get(i + 1 );
arr[index] = arr[i];
arr[i] = i + 1 ;
swap++;
index_table.put(arr[i], i);
index_table.put(arr[index], index);
}
}
return swap;
}
private static final Scanner scanner
= new Scanner(System.in);
public static void main(String[] args)
throws IOException
{
BufferedWriter bufferedWriter = new BufferedWriter(
new FileWriter(System.getenv( "OUTPUT_PATH" )));
int n = scanner.nextInt();
scanner.skip( "(\r\n|[\n\r\u2028\u2029\u0085])?" );
int [] arr = new int [n];
String[] arrItems = scanner.nextLine().split( " " );
scanner.skip( "(\r\n|[\n\r\u2028\u2029\u0085])?" );
for ( int i = 0 ; i < n; i++) {
int arrItem = Integer.parseInt(arrItems[i]);
arr[i] = arrItem;
}
int res = minimumSwaps(arr);
bufferedWriter.write(String.valueOf(res));
bufferedWriter.newLine();
bufferedWriter.close();
scanner.close();
}
}
|
Output:
Input:
4 3 1 2 5
Output:
3
Share your thoughts in the comments
Please Login to comment...