Note from Author:
Congrats to all the Winners. Glad to see so much participation. Hope to see you all participate in the next biweekly as well. You can contact me (Abhijit Mishra) or (Aditya Tyagi) for any queries related to the contest or problem discussion.
I would recommend first looking at the hints for the problem if your facing logical issue, if you are absolutely clear with logic then move on to look at the solution and the code. Efforts will be made towards making code for other languages (Python) available as well.
A — Xeros
Repeat for the n problems.
A problem will be solved by the team if there are atleast two 1s in a row. count this for every row i.e n times.
B — Best Friend Game
The remaining pens are begin divided into 2 people. So In case of no fight, the sum of the pens will be even or odd ?
Is it possible to remove a packet and make the sum of remaining even ?
When will we select the odd packet to remove ? When will we select the even packet to remove ?
If the sum is even initially then we select the even packets because if we remove a packet containing an even number of pens the sum of the remaining is even therefore no fights so the ans will be the number of even packets.
If the sum is odd initially then we select the odd packets because if we remove a packet containing an odd number of pens the sum of the remaining is even therefore no fights so the ans will be the number of even packets.
C — Happy New Year
Can two numbers be equal when the sequence is sorted in strictly increasing or decreasing order ?
Read the first hint again !
If there are two elements with the same value, then the answer is NO, because neither of these values is less than the other.
Otherwise, the answer is YES, since we can just sort the array.
The time complexity is O(nlogn) or O(n) depending on the implementation.
D — Favourite Drink
How many consecutive a are possible ?
can the letters alternate between a and b ?
Every character in strings aa, aaa, bb and bbb has at least one character adjacent to it that is the same. So, if there is an isolated character in our string (a character that has no neighbors equal to it), we cannot build it.
It's easy to see that in the other case, we can build the string: we can split it into blocks of consecutive equal characters, and since there are no isolated characters, each block will have at least 2 characters, so it can be formed from strings of length 2 and/or 3 consisting of equal characters.
So, the problem is reduced to checking if each character has a neighbor equal to it.
E — Good number
If the elements are same but the order is different, then how many operation will it take ?
which type of operation should we focus on ?
Count the number of 1's from both the sequences, if the number of ones in both the sequence is same then we can simply rearrange the sequence. else we take the absolute value of the difference + 1 (rearrangement)
F — Punishment
Bro, Seriously
What's wrong with you.
Draw the grid graph as the problem said.
Just a few basics of your programming language. It's easy.
G — Treasure
Probably world class TT players. As no datatype can hold these huge values, or atleast cannot quick operations on it.
Imagine 2 numbers 100 and 1000, here 100 < 1000 and if we divide both side by min(100,1000) then it becomes 1 < 10 , the result remains the same if we divide both numbers by some number. Think of some number to divide the powers in the given problems.
First, let's say that appending the number with p zeros is the same as multiplying it by 10^p
The given numbers are so large that they can't fit into any reasonable integer type. Even if you use a language with unlimited length integers (python, for example) or store the numbers in strings, you should still face the time limit issue. So let's learn to shrink the numbers a bit.
Note that the result of the comparison of two numbers doesn't change if you divide both numbers by the same positive number. So we can keep dividing both numbers by 10 until one of them is not divisible anymore. Let's also ignore the trailing zeros in x1 and x2 and leave them as is. If the first number is appended with p1 zeros and the second numbers is appended with p2 zeros, we can subtract min(p1,p2) from both values, effectively dividing both numbers by 10min(p1,p2)
This way, one of the numbers becomes short enough to fit into an integer type (because it has p=0 and x is only up to 10^6). The other number might still be large enough.
However, if it's really large, we can instantly say that it's larger than another one. Say, if its p is at least 7. This number it at least 10^7 and the other number is at most 10^6 Otherwise, we can calculate this number as well and compare the values normally.
Overall complexity: O(1) per testcase.
Credits: BledDest
H — Count Pairs
Is it possible if a is sorted and b is not and all elements are distinct, can we make b array sorted as well ?
If the elements are not distinct ?
Imagine that all elements of a are distinct. This way, sorting a in increasing order will fix the order of b
If b turns out sorted in a non-decreasing order, then the answer exists. Otherwise, it doesn't. To obtain the sequence of swaps, you can sort a with any comparison-based sorting algorithm you want: even bubble sort will not exceed the allowed number of swaps.
What changes if a has repeated elements? Distinct elements are still ordered among themselves, but now there are also blocks of equal elements. For each block, look into the corresponding values in b. Obviously, these have to be sorted in a non-decreasing order. Rearrange them as they should be.
In fact, this is exactly the same as sorting the sequence of pairs (ai,bi) with a default comparator — first by ai, then by bi
Since we fixed the wanted order, we can proceed with the same steps we made in a distinct case.
Overall complexity: O(nlogn) or O(n2) per testcase. Credits: BledDest
I — Transform Array
First the sequence will increase then decrease then increase again.
You can find the first position where the sequences start decreasing from the beginning. Call it L. You can find the first position where the sequences start increasing from the end. Call it R.
Now we just need to reverse the segment between a[L] to a[R].
Let us define L to be smallest index such that A[i]! = i. Let us also define R to be largest index such that A[i]! = i.
Note that if there is no such L and R, it means that array is sorted already. So answer will be "yes", we can simply reverse any of the 1 length consecutive segment.
Otherwise we will simply reverse the array from [L, R]. After the reversal, we will check whether the array is sorted or not.
Complexity: O(nlogn)