Java Century From Year
public class Solution {
public static int centuryFromYear(int year) {
return (int) Math.ceil(year / 100.0);
}
}
This converts a year into its century. Years 1-100 are century 1, 101-200 are century 2, and so on.
Java Check Palindrome
public class Solution {
public static boolean checkPalindrome(String inputString) {
return new StringBuilder(inputString).reverse().toString().equals(inputString);
}
}
This compares the string with its reverse. If they match, it is a palindrome.
Java Chocolates By Numbers
public class Solution {
public static int chocolatesByNumbers(int n, int m) {
long gcd = gcd(n, m);
long result = ((long) n * m / gcd) / m;
return (int) result;
}
private static long gcd(long n, long m) {
if (n % m == 0) {
return m;
}
return gcd(m, n % m);
}
}
This uses the greatest common divisor to figure out how many chocolates get eaten before the pattern repeats.
Java Common Prime Divisors
public class Solution {
public static int commonPrimeDivisors(int[] a, int[] b) {
int counter = 0;
for (int i = 0; i < a.length; i++) {
int x = a[i];
int y = b[i];
int d = gcd(x, y);
x = removeCommonPrimeDivisors(x, d);
if (x != 1) {
continue;
}
y = removeCommonPrimeDivisors(y, d);
if (y == 1) {
counter++;
}
}
return counter;
}
private static int gcd(int n, int m) {
if (n % m == 0) {
return m;
}
return gcd(m, n % m);
}
private static int removeCommonPrimeDivisors(int n, int m) {
while (n != 1) {
int d = gcd(n, m);
if (d == 1) {
break;
}
n /= d;
}
return n;
}
}
This checks whether two numbers are built from the same prime factors by repeatedly dividing out their shared parts.
Java Count Div
public class Solution {
public static int countDiv(int a, int b, int k) {
int firstDiv = a % k == 0 ? a : a + (k - a % k);
int lastDiv = b - b % k;
return (lastDiv - firstDiv) / k + 1;
}
}
This counts how many numbers in a range are divisible by K without looping through every value.
Java Count Factors
public class Solution {
public static int countFactors(int n) {
int count = 0;
long i = 1;
while (i * i < n) {
if (n % i == 0) {
count += 2;
}
i++;
}
if (i * i == n) {
++count;
}
return count;
}
}
This checks divisors in pairs up to the square root, which keeps the work much smaller than testing every number.
Java Count Non Divisible
import java.util.Arrays;
public class Solution {
public static int[] countNonDivisible(int[] a) {
int size = a.length;
int[] nondivisor = new int[size];
int maxValue = Arrays.stream(a).max().orElse(0);
int[] occurrences = new int[maxValue + 1];
for (int v : a) {
occurrences[v]++;
}
for (int k = 0; k < size; k++) {
int v = a[k];
int count = 0;
long i = 1;
while (i * i <= v) {
if (v % i == 0) {
count += occurrences[(int) i];
if (v / i != i) {
count += occurrences[(int) (v / i)];
}
}
i++;
}
nondivisor[k] = size - count;
}
return nondivisor;
}
}
This counts how often each value appears, then subtracts the divisor matches so you get the non-divisible count for each item.
Java Count Semi Primes
import java.util.Arrays;
public class Solution {
public static int[] countSemiPrimes(int n, int[] p, int[] q) {
boolean[] primes = new boolean[n + 1];
Arrays.fill(primes, true);
int[] semiPrimes = new int[n + 1];
int[] semiPrimeCounts = new int[p.length];
for (int i = 2; (long) i * i <= n; i++) {
if (primes[i]) {
for (int k = i * i; k <= n; k += i) {
primes[k] = false;
}
}
}
for (int k = 2; (long) k * k <= n; k++) {
if (primes[k]) {
for (int i = 2; i * k <= n; i++) {
if (primes[i]) {
semiPrimes[k * i] = 1;
}
}
}
}
for (int i = 1; i <= n; i++) {
semiPrimes[i] += semiPrimes[i - 1];
}
for (int k = 0; k < p.length; k++) {
semiPrimeCounts[k] = semiPrimes[q[k]] - semiPrimes[p[k] - 1];
}
return semiPrimeCounts;
}
}
This precomputes semiprimes and prefix sums so each range query becomes a quick subtraction.
Java Cyclic Rotation
public class Solution {
public static int[] cyclicRotation(int[] a, int k) {
if (a.length > 0) {
for (int i = 0; i < k; i++) {
int last = a[a.length - 1];
System.arraycopy(a, 0, a, 1, a.length - 1);
a[0] = last;
}
}
return a;
}
}
This rotates the array to the right by K steps and keeps the wrap-around values in the correct order.
Java Distinct
import java.util.Arrays;
public class Solution {
public static int distinct(int[] a) {
return (int) Arrays.stream(a).distinct().count();
}
}
This counts unique values by tracking what has already been seen.