Overview
A small archive of HackerRank problems I solved and synced to GitHub. Each problem has its own folder with the problem statement (README.md) and my solution. It's four problems, and I've since kept regular practice in my LeetCode and online-assessment prep repos, which go much further.
Problems
Equalize the Array (C++)
Given an array, find the minimum number of deletions so that every remaining element is equal. The answer is the array length minus the frequency of the most common value. The solution counts occurrences in a std::map, takes the maximum count and subtracts it from the length: O(n log k) time for k distinct values. An unordered_map would make it O(n) on average.
Find Digits (C++)
For an integer n, count how many of its digits divide n exactly. The solution converts n to a string, walks its characters, skips zeros to avoid division by zero and tests n % d == 0. Time is proportional to the number of digits. Repeated % 10 and / 10 would do the same without allocating a string.
Java Primality Test (Java)
Decide whether a very large number is prime. The solution parses the input into a BigInteger and calls isProbablePrime, the right tool when the input exceeds long. One thing I'd change: it passes a certainty of 1, which only bounds the error at 1/2. Production code should pass something like 50 to 100 so the false-positive probability is negligible.
Java String Tokens (Java)
Split a string into tokens on any run of non-letter characters and print the count followed by each token. The solution trims the line and splits on the Unicode class regex \P{Alpha}+. It checks for an empty first token with ==, which compares references rather than contents in Java, so isEmpty() is the correct test. A leading delimiter also produces an empty first token that should be filtered out.
Built with
- C++ (Standard Library containers)
- Java (
java.math.BigInteger, regex-based tokenising)