1. Estimating Time Complexity Before Writing Line One
Online judges (including the ArkyEdu Olympiad Arena) execute roughly 10^7 to 10^8 simple operations per second. Before picking an approach, always inspect the maximum constraint on N!
| Input Constraint N | Target Complexity | Typical C++ Pattern | Example Algorithm |
|---|---|---|---|
| N ≤ 1,000 | O(N²) | Two nested for loops | Bubble Sort, Selection Sort, checking all pairs (i, j) |
| N ≤ 100,000 | O(N log N) or O(N) | Fast sorting, Binary Search, Sieve | M binary-search queries, Sieve of Eratosthenes |
| N ≤ 1,000,000 | O(N) | Single linear scan | Min/Max tracking, Frequency arrays, Prefix sums |
| N ≤ 10⁹ or 10¹² | O(√N), O(log N), or O(1) | Math, divisors up to √N, Gauss sum | O(√N) primality, Euclidean GCD, arithmetic series |
If n is near 2 billion (2·10^9) and your loop writes for (int d = 3; d * d <= n; d += 2), d * d can overflow 32-bit signed int (max 2,147,483,647) and wrap negative! Always use long long d or 1LL * d * d <= n.
2. O(√N) Primality Testing & The Sieve of Eratosthenes
Beginners test primality by dividing n by every number up to n - 1, taking a million steps when n = 10^6. Because divisors always come in pairs (d, n/d), at least one factor must be ≤ √n—cutting the work down to ~1,000 steps.
When asked to check tens of thousands of numbers up to 1,000,000, precomputing the Sieve of Eratosthenes answers every single query in O(1) instant lookup!
bool isPrime(long long n) {
if (n < 2) return false;
if (n == 2) return true;
if (n % 2 == 0) return false;
for (long long d = 3; d * d <= n; d += 2) {
if (n % d == 0) return false;
}
return true;
}
// Precompute primes up to 100,000 for instant O(1) queries
const int MAXV = 100000;
bool isComposite[MAXV + 1]; // global => zero-initialized (false)
void buildSieve() {
isComposite[0] = isComposite[1] = true;
for (int i = 2; i * i <= MAXV; i++) {
if (!isComposite[i]) {
for (int j = i * i; j <= MAXV; j += i) {
isComposite[j] = true;
}
}
}
}
3. Frequency Arrays: Turning Nested Loops into Linear O(N) Speed
A classic contest task asks: „Given N integers between 0 and 1,000 (or digits 0..9), print the most frequent value or output them sorted.”
Instead of O(N²) comparisons, use each input value x directly as the array index in a global counter array: freq[x]++. You get instant counting and sorted traversal in linear time!
#include <iostream>
using namespace std;
int freq[1001]; // global => automatically initialized to 0!
int main() {
int n, x;
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> x;
if (x >= 0 && x <= 1000) freq[x]++;
}
int bestVal = 0, maxCount = -1;
for (int v = 0; v <= 1000; v++) {
if (freq[v] > maxCount) {
maxCount = freq[v];
bestVal = v;
}
}
cout << bestVal << " appears " << maxCount << " times\n";
return 0;
}
4. O(log N) Binary Search Without Infinite Loops
Binary search requires a sorted array and cuts the search range in half every step—finding any target among 1,000,000 numbers in at most 20 comparisons.
Most bugs happen when students write left = mid or right = mid instead of mid + 1 / mid - 1, trapping a 2-element window in an infinite loop.
// Returns index of x in sorted array a[1..n], or -1 if absent
int binarySearch(int a[], int n, int x) {
int left = 1, right = n;
while (left <= right) {
int mid = left + (right - left) / 2; // prevents overflow
if (a[mid] == x) return mid;
if (a[mid] < x) left = mid + 1;
else right = mid - 1;
}
return -1;
}
// First index where a[idx] >= x
int lowerBoundIdx(int a[], int n, int x) {
int left = 1, right = n, ans = -1;
while (left <= right) {
int mid = left + (right - left) / 2;
if (a[mid] >= x) {
ans = mid;
right = mid - 1; // keep searching to the left!
} else {
left = mid + 1;
}
}
return ans;
}
5. The 5-Step Contest Habit & Visual Trace Debugging
Even great algorithms lose points on edge cases (N = 1, x = 0, duplicate numbers, or integer overflow). Build these five contest habits early:
- Check numeric bounds first: if a running sum or product can exceed 2·10^9, declare it long long immediately.
- Dry-run edge cases on paper: verify N = 0, N = 1, all elements equal, and target smaller/larger than every element.
- Keep large arrays global with a small safety buffer (+10 slots above the stated max N).
- Step through the first 3 iterations manually (or in ArkyEdu C++ Visual Trace) to watch indices and loop exits.
- Never leave prompt text like "Enter N: " in cout—automated judges compare raw output tokens strictly.