Вы находитесь на странице: 1из 7

1) Checking if the number is even or odd without using the BufferedReader – (fast, but not recommended as it requires

% operator: lot of typing): The Java.io.BufferedReader class reads text


Although this trick is not much better than using % operator but from a character-input stream, buffering characters so as to
is sometimes efficient (with large numbers). Use & operator: provide for the efficient reading of characters, arrays, and
lines. With this method we will have to parse the value every
time for desired type. Reading multiple words from single line
Example: adds to its complexity because of the use of Stringtokenizer
num = 5 and hence this is not recommended. This gets accepted with a
Binary: “101 & 1” will be 001, so true running time of approx 0.89 s.but still as you can see it
num = 4 requires a lot of typing all together and therefore method 3 is
Binary: “100 & 1” will be 000, so false. recommended.
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 Fast I/O in Java in Competitive Programming

Using Java in competitive programming is not something many


people would suggest just because of its slow input and
output, and well indeed it is slow. 5) Calculating the most significant digit: To calculate the
In this article, we have discussed some ways to get around the most significant digit of any number log can be directly used to
difficulty and change the verdict from TLE to (in most cases) calculate it.
AC. Suppose the number is N then

For all the Programs below Let double K = Math.log10(N);


Scanner Class – (easy, less typing, but not recommended
now K = K - Math.floor(K);
very slow, refer this for reasons of slowness): In most of the
cases we get TLE while using scanner class. It uses built-in int X = (int)Math.pow(10, K);
nextInt(), nextLong(), nextDouble methods to read the desired
object after initiating scanner object with input stream.(eg X will be the most significant digit.
System.in). The following program many a times gets time limit
exceeded verdict and therefore not of much use. 6) 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;

7 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.

8. String vs StringBuilder vs StringBuffer in Java

Prerequisites : String , Initialize a String


Consider below code with three concatenation functions with
three different types of parameters, String, StringBuffer and
StringBuilder.
8) Inbuilt GCD Method: Java has inbuilt GCD method
in BigInteger class. It returns a BigInteger whose value is the 9) checking for a prime number: Java has inbuilt
greatest common divisor of abs(this) and abs(val). Returns 0 if isProbablePrime() method in BigInteger class. It returns true if
this==0 && val==0. 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.

11) Sorting Algorithm:


Arrays.sort() used to sort elements of a 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 an 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 frequency of
specified element in a collection or an array.
15) Most data structures are already implemented in
the Collections Framework. For example
: Stack, LinkedList, HashSet, HashMaps, Heaps etc.
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.

Output: QuickSort

Binary representation of A : 1000001101 Like Merge Sort, QuickSort is a Divide and Conquer algorithm.
It picks an element as pivot and partitions the given array
Binary representation of B : around the picked pivot. There are many different versions of
1011100110011101111100110101101101 quickSort that pick pivot in different ways.
1. Always pick first element as pivot.
Octal representation of A : 1015
2. Always pick last element as pivot (implemented below)
Octal representation of B : 134635746555 3. Pick a random element as pivot.
4. Pick median as pivot.
Binary Search: Search a sorted array by repeatedly dividing The key process in quickSort is partition(). Target of partitions
the search interval in half. Begin with an interval covering the is, given an array and an element x of array as pivot, put x at
whole array. If the value of the search key is less than the item its correct position in sorted array and put all smaller elements
in the middle of the interval, narrow the interval to the lower (smaller than x) before x, and put all greater elements (greater
half. Otherwise narrow it to the upper half. Repeatedly check than x) after x. All this should be done in linear time.
until the value is found or the interval is empty. Pseudo Code for recursive QuickSort function :
Java.util.Scanner.hasNext() Method
Description
The java.util.Scanner.hasNext() method Returns true if this
scanner has another token in its input. This method may block
while waiting for input to scan. The scanner does not advance
past any input.
Declaration
Following is the declaration
for java.util.Scanner.hasNext() method
public boolean hasNext()
Parameters
NA
Return Value
This method returns true if and only if this scanner has another
token
Exception
IllegalStateException − if this scanner is closed
Example
The following example shows the usage of
java.util.Scanner.hasNext() method.

Let us compile and run the above program, this


will produce the following result −
Method Summary
static asList(T... a)
<T> List<T> Returns a fixed-size list backed by the specified array.
static int binarySearch(byte[] a, byte key)
Searches the specified array of bytes for the specified value using the binary search algorithm.
static int binarySearch(char[] a, char key)
Searches the specified array of chars for the specified value using the binary search algorithm.
static int binarySearch(double[] a, double key)
Searches the specified array of doubles for the specified value using the binary search algorithm.
static int binarySearch(float[] a, float key)
Searches the specified array of floats for the specified value using the binary search algorithm.
static int binarySearch(int[] a, int key)
Searches the specified array of ints for the specified value using the binary search algorithm.
static int binarySearch(long[] a, long key)
Searches the specified array of longs for the specified value using the binary search algorithm.
static int binarySearch(Object[] a, Object key)
Searches the specified array for the specified object using the binary search algorithm.
static int binarySearch(short[] a, short key)
Searches the specified array of shorts for the specified value using the binary search algorithm.
static binarySearch(T[] a, T key, Comparator<? super T> c)
<T> int Searches the specified array for the specified object using the binary search algorithm.
static boolean deepEquals(Object[] a1, Object[] a2)
Returns true if the two specified arrays are deeply equal to one another.
static int deepHashCode(Object[] a)
Returns a hash code based on the "deep contents" of the specified array.
static String deepToString(Object[] a)
Returns a string representation of the "deep contents" of the specified array.
static boolean equals(boolean[] a, boolean[] a2)
Returns true if the two specified arrays of booleans are equal to one another.
static boolean equals(byte[] a, byte[] a2)
Returns true if the two specified arrays of bytes are equal to one another.
static boolean equals(char[] a, char[] a2)
Returns true if the two specified arrays of chars are equal to one another.
static boolean equals(double[] a, double[] a2)
Returns true if the two specified arrays of doubles are equal to one another.
static boolean equals(float[] a, float[] a2)
Returns true if the two specified arrays of floats are equal to one another.
static boolean equals(int[] a, int[] a2)
Returns true if the two specified arrays of ints are equal to one another.
static boolean equals(long[] a, long[] a2)
Returns true if the two specified arrays of longs are equal to one another.
static boolean equals(Object[] a, Object[] a2)
Returns true if the two specified arrays of Objects are equal to one another.
static boolean equals(short[] a, short[] a2)
Returns true if the two specified arrays of shorts are equal to one another.
static void fill(boolean[] a, boolean val)
Assigns the specified boolean value to each element of the specified array of booleans.
static void fill(boolean[] a, int fromIndex, int toIndex, boolean val)
Assigns the specified boolean value to each element of the specified range of the specified array of booleans.
static void fill(byte[] a, byte val)
Assigns the specified byte value to each element of the specified array of bytes.
static void fill(byte[] a, int fromIndex, int toIndex, byte val)
Assigns the specified byte value to each element of the specified range of the specified array of bytes.
static void fill(char[] a, char val)
Assigns the specified char value to each element of the specified array of chars.
static void fill(char[] a, int fromIndex, int toIndex, char val)
Assigns the specified char value to each element of the specified range of the specified array of chars.
static void fill(double[] a, double val)
Assigns the specified double value to each element of the specified array of doubles.
static void fill(double[] a, int fromIndex, int toIndex, double val)
Assigns the specified double value to each element of the specified range of the specified array of doubles.
static void fill(float[] a, float val)
Assigns the specified float value to each element of the specified array of floats.
static void fill(float[] a, int fromIndex, int toIndex, float val)
Assigns the specified float value to each element of the specified range of the specified array of floats.
static void fill(int[] a, int val)
Assigns the specified int value to each element of the specified array of ints.
static void fill(int[] a, int fromIndex, int toIndex, int val)
Assigns the specified int value to each element of the specified range of the specified array of ints.
static void fill(long[] a, int fromIndex, int toIndex, long val)
Assigns the specified long value to each element of the specified range of the specified array of longs.
static void fill(long[] a, long val)
Assigns the specified long value to each element of the specified array of longs.
static void fill(Object[] a, int fromIndex, int toIndex, Object val)
Assigns the specified Object reference to each element of the specified range of the specified array of Objects.
static void fill(Object[] a, Object val)
Assigns the specified Object reference to each element of the specified array of Objects.
static void fill(short[] a, int fromIndex, int toIndex, short val)
Assigns the specified short value to each element of the specified range of the specified array of shorts.
static void fill(short[] a, short val)
Assigns the specified short value to each element of the specified array of shorts.
static int hashCode(boolean[] a)
Returns a hash code based on the contents of the specified array.
static int hashCode(byte[] a)
Returns a hash code based on the contents of the specified array.
static int hashCode(char[] a)
Returns a hash code based on the contents of the specified array.
static int hashCode(double[] a)
Returns a hash code based on the contents of the specified array.
static int hashCode(float[] a)
Returns a hash code based on the contents of the specified array.
static int hashCode(int[] a)
Returns a hash code based on the contents of the specified array.
static int hashCode(long[] a)
Returns a hash code based on the contents of the specified array.
static int hashCode(Object[] a)
Returns a hash code based on the contents of the specified array.
static int hashCode(short[] a)
Returns a hash code based on the contents of the specified array.
static void sort(byte[] a)
Sorts the specified array of bytes into ascending numerical order.
static void sort(byte[] a, int fromIndex, int toIndex)
Sorts the specified range of the specified array of bytes into ascending numerical order.
static void sort(char[] a)
Sorts the specified array of chars into ascending numerical order.
static void sort(char[] a, int fromIndex, int toIndex)
Sorts the specified range of the specified array of chars into ascending numerical order.
static void sort(double[] a)
Sorts the specified array of doubles into ascending numerical order.
static void sort(double[] a, int fromIndex, int toIndex)
Sorts the specified range of the specified array of doubles into ascending numerical order.
static void sort(float[] a)
Sorts the specified array of floats into ascending numerical order.
static void sort(float[] a, int fromIndex, int toIndex)
Sorts the specified range of the specified array of floats into ascending numerical order.
static void sort(int[] a)
Sorts the specified array of ints into ascending numerical order.
static void sort(int[] a, int fromIndex, int toIndex)
Sorts the specified range of the specified array of ints into ascending numerical order.
static void sort(long[] a)
Sorts the specified array of longs into ascending numerical order.
static void sort(long[] a, int fromIndex, int toIndex)
Sorts the specified range of the specified array of longs into ascending numerical order.
static void sort(Object[] a)
Sorts the specified array of objects into ascending order, according to the natural ordering of its elements.
static void sort(Object[] a, int fromIndex, int toIndex)
Sorts the specified range of the specified array of objects into ascending order, according to the natural ordering of
its elements.
static void sort(short[] a)
Sorts the specified array of shorts into ascending numerical order.
static void sort(short[] a, int fromIndex, int toIndex)
Sorts the specified range of the specified array of shorts into ascending numerical order.
static sort(T[] a, Comparator<? super T> c)
<T> void Sorts the specified array of objects according to the order induced by the specified comparator.
static sort(T[] a, int fromIndex, int toIndex, Comparator<? super T> c)
<T> void Sorts the specified range of the specified array of objects according to the order induced by the specified
comparator.
static String toString(boolean[] a)
Returns a string representation of the contents of the specified array.
static String toString(byte[] a)
Returns a string representation of the contents of the specified array.
static String toString(char[] a)
Returns a string representation of the contents of the specified array.
static String toString(double[] a)
Returns a string representation of the contents of the specified array.
static String toString(float[] a)
Returns a string representation of the contents of the specified array.
static String toString(int[] a)
Returns a string representation of the contents of the specified array.
static String toString(long[] a)
Returns a string representation of the contents of the specified array.
static String toString(Object[] a)
Returns a string representation of the contents of the specified array.
static String toString(short[] a)
Returns a string representation of the contents of the specified array.

Вам также может понравиться