C++ Century From Year
#include <cmath>
int centuryFromYear(int year)
{
return static_cast<int>(std::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.
C++ Check Palindrome
#include <string>
bool checkPalindrome(const std::string& inputString)
{
return std::string(inputString.rbegin(), inputString.rend()) == inputString;
}
This compares the string with its reverse. If they match, it is a palindrome.
C++ Chocolates By Numbers
long long chocolatesGcd(long long n, long long m)
{
if (n % m == 0) {
return m;
}
return chocolatesGcd(m, n % m);
}
long long chocolatesByNumbers(long long n, long long m)
{
long long g = chocolatesGcd(n, m);
return ((n * m) / g) / m;
}
This uses the greatest common divisor to figure out how many chocolates get eaten before the pattern repeats.
C++ Common Prime Divisors
#include <cstddef>
#include <vector>
long long commonPrimeDivisorsGcd(long long n, long long m)
{
if (n % m == 0) {
return m;
}
return commonPrimeDivisorsGcd(m, n % m);
}
long long removeCommonPrimeDivisors(long long n, long long m)
{
while (n != 1) {
long long d = commonPrimeDivisorsGcd(n, m);
if (d == 1) {
break;
}
n /= d;
}
return n;
}
int commonPrimeDivisors(const std::vector<int>& a, const std::vector<int>& b)
{
int counter = 0;
for (std::size_t i = 0; i < a.size(); ++i) {
long long x = a[i];
long long y = b[i];
long long d = commonPrimeDivisorsGcd(x, y);
x = removeCommonPrimeDivisors(x, d);
if (x != 1) {
continue;
}
y = removeCommonPrimeDivisors(y, d);
if (y == 1) {
++counter;
}
}
return counter;
}
This checks whether two numbers are built from the same prime factors by repeatedly dividing out their shared parts.
C++ Count Div
long long countDiv(long long a, long long b, long long k)
{
long long firstDiv = a % k == 0 ? a : a + (k - a % k);
long long 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.
C++ Count Factors
long long countFactors(long long n)
{
long long count = 0;
long 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.
C++ Count Non Divisible
#include <algorithm>
#include <vector>
std::vector<int> countNonDivisible(const std::vector<int>& a)
{
int size = static_cast<int>(a.size());
std::vector<int> nondivisor(size, 0);
int maxVal = *std::max_element(a.begin(), a.end());
std::vector<int> occurrences(maxVal + 1, 0);
for (int v : a) {
++occurrences[v];
}
for (int k = 0; k < size; ++k) {
int v = a[k];
int count = 0;
long long i = 1;
while (i * i <= v) {
if (v % i == 0) {
count += occurrences[i];
if (v / i != i) {
count += occurrences[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.
C++ Count Semi Primes
#include <cstddef>
#include <vector>
std::vector<int> countSemiPrimes(int n, const std::vector<int>& p, const std::vector<int>& q)
{
std::vector<bool> primes(n + 1, true);
std::vector<int> semiPrimes(n + 1, 0);
std::vector<int> semiPrimeCounts(p.size(), 0);
for (int i = 2; i * i <= n; ++i) {
if (primes[i]) {
for (int k = i * i; k <= n; k += i) {
primes[k] = false;
}
}
}
for (int k = 2; 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 (std::size_t k = 0; k < p.size(); ++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.
C++ Cyclic Rotation
#include <vector>
std::vector<int> cyclicRotation(std::vector<int> a, int k)
{
if (!a.empty()) {
for (int i = 0; i < k; ++i) {
int back = a.back();
a.pop_back();
a.insert(a.begin(), back);
}
}
return a;
}
This rotates the array to the right by K steps and keeps the wrap-around values in the correct order.
C++ Distinct
#include <unordered_set>
#include <vector>
int distinct(const std::vector<int>& a)
{
std::unordered_set<int> unique(a.begin(), a.end());
return static_cast<int>(unique.size());
}
This counts unique values by tracking what has already been seen.