Missing number
The first solution which might come to our mind is to allocate a hash table of size n, iterate through
the input, and mark each number as “seen” in the table. Then go through the numbers from 1..n and check if they were seen in the first step.
This works, but it’s overkill in this case. Since we know that there is only one number missing from 1 to n,
and all the others are present only once, we can apply another solution. The sum of numbers from 1 to n is (n*(n+1))/2. If we calculate this sum, and subtract each number from the input from it, the remaining number is exactly the number we are looking for. The advantage of this technique is that we only iterate through 1..n once, when reading the input, and we also allocate much less memory as there is no need for a hash table.
long n;
cin >> n;
long sum = ((n * (n + 1)) / 2);
while (--n) {
int nr;
cin >> nr;
sum -= nr;
}
cout << sum << "\n";
Sum of two values
Although it was probably “expected”, I didn’t solve this problem by sorting the input array first. Having a sorted array, we could use a two-pointer technique, but more on that in a future article, I’m sure there will be problems where we will need to use it :)
Instead, I was using a hash table, with the following logic: iterating through the input array, check if the target number minus the current number is in the table; if not, store the current number as the key and its index as the value; otherwise print the current index and the value found in the table.
int i = 1;
while (i <= n) {
int k;
cin >> k;
if (m.find(target-k) == m.end()) {
m[k] = i;
} else {
cout << m[target-k] << ' ' << i << "\n";
break;
}
i++;
}
cout << "IMPOSSIBLE\n";
Gray code
Probably one of the easiest problems of the set, and there is not much explanation needed here. I used this formula to generate the Gray codes.
for (int k = 0; k < pow(2, n); k++) {
string s = bitset<16>(k ^ (k >> 1)).to_string();
cout << s.substr(16-n, 16) << endl;
}
Exponentiation
By definition, the result of “b raised to the power of n” is given by multiplying b with itself n times. We could solve the problem this way, but since the exponent can be a very large number, going through all the multiplications could be quite costly.
We can do better though. Our goal is to reduce the number of multiplications, so let’s try to break up the problem into sub-problems so that “merging” the sub-problems can be done with only one multiplication. This is easy, just think about what’s the easiest way to calculate 2^20 for example. We can calculate 2^10 instead, and multiply the result with itself to get 2^20. Then calculate 2^10 by calculating 2^5 first and then multiply it with itself. You can see where this is going, instead of having an operation with O(n) complexity, we can have one with O(log n) complexity. Much better! There is only one caveat. When we reach odd exponents, for example in the case of 2^5, we don’t want to calculate 2^(5/2) as it would make things more complicated. Instead, we can calculate 2^2, multiply it with itself, and multiply again with 2. Generally, speaking, when n is odd, we calculate x = b^((n-1)/2) and return x * x * b. Let’s just look at the code to make this clearer.
long long power(long long a, long long b) {
if (b == 0) {
return 1;
}
if (b == 1) {
return a % MOD;
}
long long result;
long long x = power(a, b / 2);
result = (x * x) % MOD;
if (b % 2 == 1) {
result *= a;
result %= MOD;
}
return result;
}
Fibonacci numbers
When I first saw this problem, I was wondering why there were relatively few people who had solved it, despite it being a classical computer science problem. Then I saw the upper boundary of n, and I realized that the well-known dynamic programming solution will not be enough here. Luckily, we can find a solution similar to the exponentiation problem. Let’s see the formula of the Fibonacci numbers:
F(n) = F(n-1) + F(n-2)
Now, if we replace F(n-1) with F(n-2) + F(n-3), we get:
F(n) = 2 * F(n-2) + F(n-3)
Let’s replace F(n-2) now with F(n-3) + F(n-4):
F(n) = 3 * F(n-3) + 2 * F(n-4)
If we do this again and again, we realize something interesting. The factors in the two terms of this formula are Fibonacci numbers! Generally, we can write the following:
F(n) = F(k) * F(n-k+1) + F(k-1) * F(n-k), for every k between 1..n.
Since we want to arrive at an O(log n) solution, we try to calculate a F(n) by using F(n/2). So let’s try and replace k with n/2 in our last formula:
F(n) = F(n/2) * F(n/2+1) + F(n/2-1) * F(n/2), which can be simplified as:
F(n) = F(n/2) * (F(n/2+1) + F(n/2-1))
This is great! We were able to find a recursive formula which doesn’t require us to calculate all the Fibonacci numbers, but only a couple of them. One thing to be noted is that this formula only works when n is even, otherwise F(n/2) doesn’t make sense. But a similar formula can be worked out by replacing k with (n+1)/2.
There is nothing else left now, but write a recursive algorithm which uses these formulas, and we are done!
long long MOD = 1e9 + 7;
long long fibo(long long n) {
if (m.find(n) == m.end()) {
ll s;
if (n % 2 == 0) {
ll k = n/2;
s = fibo(k) * ((fibo(k-1) + fibo(k+1)) % MOD);
} else {
ll k = (n+1)/2;
s = (fibo(k) * fibo(k)) % MOD + (fibo(k-1) * fibo(k-1) % MOD);
}
m[n] = s % MOD;
cout << n << ' ' << m[n] << endl;
}
return m[n];
}