Here are my solutions. I have all correct solution, I'll explain few of interesting problems, if you want other answers simply comment.
Coding Problem - Q17
Its a simple dp problem with certain edge cases easy to fall upon. Simply maintain frequency of each character of S1 in S2 online as you iterate through S2 from left to right. Comment if something is not clear in code.
int f[3]; // frequency of each character in s1
long solve(string s1, string s2) {
for (int i = 0; i < s2.length(); i++) f[2] += (s2[i]==s1[2]?f[1]:0), f[1] += (s2[i]==s1[1]?f[0]:0), f[0] += (s2[i]==s1[0]?1:0);
return f[2];
} //Yes, code is over rofl
Coding Problem - Q18
Even easier problem, the only irritating part is string parsing in query. We simply check if i
th string starts and ends with vowel, if you yes we store 1 in that position else 0. Simply construct prefix on this answer and now you can return sum of ranges in O(1).
int prefix[200002];
vector solve(vector sarray, vector queries) {
for (int i = 0; i < sarray.size(); i++) {
bool j1 = false, j2 = false;
for (int j = 0; j < 5; j++) j1 = (sarray[i][0]==vowel[j]?true:j1), j2 = (sarray[i][0]==vowel[j]?true:j2);
if (j1 and j2) prefix[i+1] = 1; else prefix[i+1] = 0;
} vector Sol;
for (int i = 0; i < sarray.size(); i++) prefix[i+1] += prefix[i];
for (int i = 0; i < queries.size(); i++) {
string Left = "", Right = ""; int j = 0;
while (queries[i][j] != '-') Left += queries[i][j++];
for (++j; j < queries[i].length(); j++) Right += queries[i][j];
Sol.push_back(prefix[stoi(Right)] - prefix[stoi(Left)-1]);
} return Sol;
}
Puzzles
15. Infection Chessboard
Intuitively, keep all diagonals infected initially, it will surely cover whole board. I have a complicated proof of optimality but i guess, intuition is enough.
16. Lying
100
th person is surely lying, since if all 100 are lying then 100th is lying too, which means he should be saying truth, which is contradiction.
So this implies, 1st person is saying truth.
Now this implies 99th person is lying, which in turn implies 2nd person is truthful and this chain goes on.
So in conclusion first 50 are saying truth, and last 50 are lying.
13. Light Bulb
So i
th bulb will be switched exactly
D number of times, where D(i) is number of divisors of
i.
Initially all bulbs were off, so if D(i) is odd only then shall this bulb remain on at end.
If N = p
a x q
b x r
c where p,q,r are prime numbers then D(i) is (a+1)(b+1)(c+1). We want D(i) to be odd, so each term has to be odd. This means every exponent a, b, c should be even. This implies N is a square number!
So only bulbs which are square numbers will be on, that is bulb 1, 4, 9, 16, 25,.., 100 will remain on. so squar_root(100) =
10 is number of bulbs that remains on at end of process.
14. Quadratic Equation
ax2 + bx + c
a,b,c belongs to {0,1,...,33}.
If a = 0, then f(2020) cannot be any of 4 options. For a = 1 its possible. For a = 2, even with b=c=0, f(2020) is greater than all 4 options.
=> a = 1.
So now our quadratic is
x2 + bx + c
.
Now we utilize f(40) = 2020.
f(40) = 2020 = 1600 + b * 40 + c
=> 420 = b * 40 + c. [ 0 <= b,c <= 33 ].
Clearly b <= 10, as 11*40 > 420 and c has to be atleast 0.
Now c can be only in {0, 10, 20, 30}. as b * 40 is divisible by 10.
Checking these, we find b = 10, c = 20.
x2 + 10 x + 20. This is final quadratic.
Now simply, put f(2020) = 4100620.
It should be f[0] += (s2[i]==s1[0]?1:0);
Problems are rather straightforward. Does it really matter :P
Haha. Yes, its still helpful to know MCQs - cutoffs will be decided by that and some of them are good.