Although the practice is the only way that ensures increased performance in programming contests but having some tricks up your sleeve ensures an upper edge and fast debugging.
1) Checking if the number is even or odd without using the % operator:
Although this trick is not much better than using a % operator but is sometimes efficient (with large numbers).
Use & operator:
System.out.println((a & 1) == 0 ? "EVEN" : "ODD" );
num = 5 Binary: "101 & 1" will be 001, so false num = 4 Binary: "100 & 1" will be 000, so true.
2) Fast Multiplication or Division by 2
Multiplying by 2 means shifting all the bits to left and dividing by 2 means shifting to the right.
Example: 2 (Binary 10): shifting left 4 (Binary 100) and right 1 (Binary 1)
n = n << 1; // Multiply n with 2 n = n >> 1; // Divide n by 2
3) Swapping of 2 numbers using XOR:
This method is fast and doesn’t require the use of the 3rd variable.
// A quick way to swap a and b a ^= b; b ^= a; a ^= b;
4) Faster I/O:
Refer here for Fast I/O in java
5) For String manipulations:
6) Calculating the most significant digit:
To calculate the most significant digit of any number log can be directly used to calculate it.
Suppose the number is N then Let double K = Math.log10(N); now K = K - Math.floor(K); int X = (int)Math.pow(10, K); X will be the most significant digit.
7) Calculating the number of digits directly:
To calculate the number of digits in a number, instead of looping we can efficiently use log :
No. of digits in N = Math.floor(Math.log10(N)) + 1;
8) Inbuilt GCD Method:
Java has inbuilt GCD method in BigInteger class. It returns a BigInteger whose value is the greatest common divisor of abs(this) and abs(val). Returns 0 if this==0 && val==0.
public BigInteger gcd(BigInteger val) Parameters : val - value with which the GCD is to be computed. Returns : GCD(abs(this), abs(val))
Below is the implementation of GCD:
9) checking for a prime number:
Java has an inbuilt isProbablePrime() method in BigInteger class. It returns true if this BigInteger is probably prime(with some certainty), false if it’s definitely composite.
The normal technique of division the complexity comes out to be O(logN), but it can be solved using O(v) where v is the number of digits of the number in binary form.
11) Sorting Algorithm:
- Arrays.sort() used to sort elements of an array.
- Collections.sort() used to sort elements of a collection.
For primitives, Arrays.sort() uses dual pivot quicksort algorithms.
12) Searching Algorithm:
- Arrays.binarySearch()(SET 1 | SET2) used to apply binary search on a sorted array.
- Collections.binarySearch() used to apply binary search on a collection based on comparators.
13) Copy Algorithm:
- Arrays.copyOf() and copyOfRange() copy the specified array.
- Collections.copy() copies specified collection.
14) Rotation and Frequency
We can use Collections.rotate() to rotate a collection or an array by a specified distance. You can also use Collections.frequency() method to get the frequency of a specified element in a collection or an array.
15) Most data structures are already implemented in the Collections Framework.
16) Use Wrapper class functions for getting radix conversions of a number Sometimes you require radix conversion of a number. For this, you can use wrapper classes.
Binary representation of A : 1000001101 Binary representation of B : 1011100110011101111100110101101101 Octal representation of A : 1015 Octal representation of B : 134635746555
This article is contributed by Gaurav Miglani. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready. Get hold of all the important mathematical concepts for competitive programming with the Essential Maths for CP Course at a student-friendly price.
In case you wish to attend live classes with industry experts, please refer Geeks Classes Live