Competitive Programming with Java : Helper functions 01

Hello,

Java standard library provides a lot of helper methods to deal with common tasks in Competitive programming such as Parsing, Sorting, Searching …. today we are going to

take a look the most used ones :

Integer.parseInt(String  number)
Integer.parseInt(String number, int radix) 

Converts the String given as parameter to Integer , if a radix is given it converts to the specific given base,  it not, it uses base 10 .

Integer.toBinaryString(int number)

Returns a String , the Binary representation of the given number .

Integer.toHexString(int number)

Returns a String , the Hexadecimal representation of the given number .

Integer.toOctalString(int number)

Returns a String , the Octal representation of the given number .

Integer.bitCount(int number)

returns the number of bits set to one in the binary representation of the given number .

Integer.compare(int first, int second)

returns a negative value if the first is smaller than the second, a positive value if the first value is bigger, 0 otherwise .

Integer.numberOfLeadingZeros(int number)

returns the number of zero bits preceding the left most one bit .

Integer.numberOfTrailingZeros(int number)

returns the number of zero bits following the least significant 1 bit .

Note : all of the methods above are given in Long class .

String.join(CharSequence delimiter, CharSequence... elements)

returns a String representing the elements of the array joined by the delimiter , a CharSequence is a Parent class Of String, which means you can use this method only on String elements .

String.valueOf(char[] arr, int offset, int count)

returns a String constructed from the arr variable, it contains elements starting from offset to offset + count .

String.format(String format, Object... args)

it’s like using System.out.printf but instead of printing to the console, this one returns the string , you can use a multiple formats as defined in the following table .

%c character
%d decimal (integer) number (base 10)
%e exponential floating-point number
%f floating-point number
%i integer (base 10)
%o octal number (base 8)
%s a string of characters
%u unsigned decimal (integer) number
%x number in hexadecimal (base 16)
%% print a percent sign
\% print a percent sign

Math.addExact(int a, int b)

returns the result of the addition of a and b, it throws and Exception if their is an overflow (same definition if used by longs)

 

Math.abs(int a)

return the absolute value of a .

Math.ceil(double a)

returns Returns the smallest (closest to negative infinity) value that is greater than or equal to the argument and is equal to a mathematical integer.

Math.floor(double a)

returns Returns the largest (closest to positive infinity) value that is less than or equal to the argument and is equal to a mathematical integer.

Math.sqrt(double a)

returns the square-root of a .

Math.toRadians(double a)

Converts an angle measured in degrees to an approximately equivalent angle measured in radians.

Math.toDegrees(double a)

Converts an angle measured in radians to an approximately equivalent angle measured in degrees.

Math.cos(double a), Math.sin(double a), Math.tan(double a), Math.acos(double a), Math.asin(double a), Math.atan(double a) 

the values of the Trigonometric functions cos, sin, tan, arccosine , arcsin, arctangent respectively .

Math.pow(double a, double b)

Returns the value of the first argument raised to the power of the second argument .

let’s stop here today and we will continue in the next part of the post .

Leave a comment