Although 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 % operator but is sometimes efficient (with large numbers). Use & operator:
num = 5
Binary: “101 & 1” will be 001, so true
num = 4
Binary: “100 & 1” will be 000, so false.
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)
3) Swapping of 2 numbers using XOR:
This method is fast and doesn’t require the use of 3rd variable.
4) Faster I/O:
Refer here for Fast I/O in java
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 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.
Syntax : public BigInteger gcd(BigInteger val) Parameters : val - value with which the GCD is to be computed. Returns : GCD(abs(this), abs(val))
9) checking for a prime number: Java has inbuilt isProbablePrime() method in BigInteger class. It returns true if this BigInteger is probably prime(with some certainty), false if it’s definitely composite.
10) Efficient trick to know if a number is a power of 2 The normal technique of division the complexity comes out to be O(logN), but it can be solved using O(v) where v are the number of digits of number in binary form.
For primitives, Arrays.sort() uses dual pivot quicksort algorithms.
12) Searching Algorithm:
Arrays.binarySearch()(SET 1 | SET2) used to apply binary search on an sorted array.
Collections.binarySearch() used to apply binary search on a collection based on comparators.
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 frequency of specified element in a collection or an array.
16) Use Wrapper class functions for getting radix conversions of a number Sometime 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 firstname.lastname@example.org. 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.
- Tips and Tricks for Competitive Programmers | Set 2 (Language to be used for Competitive Programming)
- Bit Tricks for Competitive Programming
- C++ tricks for competitive programming (for C++ 11)
- Fast I/O in Java in Competitive Programming
- Input/Output from external file in C/C++, Java and Python for Competitive Programming
- Input/Output from external file in C/C++, Java and Python for Competitive Programming | Set 2
- Python Tricks for Competitive Coding
- Tips and Tricks for Competitive Programmers | Set 1 (For Beginners)
- How can competitive programming help you get a job?
- How to begin with Competitive Programming?
- Fast I/O for Competitive Programming
- A Better Way To Approach Competitive Programming
- How to become a master in competitive programming?
- Python in Competitive Programming
- Knowing the complexity in competitive programming