Skip to main content

Guidelines

int numberOfDigits = (int) Math.floor(Math.log10(x));
int power = (int) Math.pow(10,numberOfDigits);


// reversing the number and checking overflow
while(x != 0){
int rem = x % 10;
x /= 10;
if (num > Integer.MAX_VALUE/10 || (num == Integer.MAX_VALUE / 10 && rem > 7)) return 0;
num = num * 10 + rem;
}

// if digit is presented with a char
num = (num * 10) + (c - '0');

we can formulate recursive relation for pow operation pow(x, n) = pow(x^n/2) * pow(x^n/2) for even n (e.g. x^4 = x^2 * x^2), while for odd n we have the following pow(x, n) = pow(x^n/2) * pow(x^n/2) * x (e.g. x^5 = x^2 * x^2 * x).

// get random element from the list
Random rnd = new Random();
list.get(rnd.nextInt(list.size()));