Data hiding and encapsulation, this concepts are never explained in any text beyond the definition and people often misunderstood this concept therefore I have made a video on youTube in which I have explained actual meaning of this concept in detail and how important this concept is. I also make videos on demand, if you find difficulty in understanding anything in java.
Real meaning of data hiding and encapsulation(Beginners+Expert)
I think I found a loop hole in MAKETRI February Long Challenge 2017 question's test cases?
Consider the following code, which gets AC with 100 points for the question. The code can be found here:
https://www.codechef.com/viewsolution/13056645
Now, consider the following test case:
3 500 700 1 5 15
The intervals formed by the numbers are [5, 5] and [11,19]. Both these intervals are not present in the [L,R] range of [500,700]. Which implies that the answer for the above test case must be 0.
However, if you were to feed in that test case to the code solution in the link above, it gives 1. Which is wrong.
So, if that test case was included in the original contest, some submissions that got an AC would have got a WA.
Please correct me if I missed out on any constraints, etc and there isn't a loop hole.
Does CodeChef have any Code Of Conduct?
I am new to CodeChef. I am not sure what can be considered as an improper conduct on this website. I do not want to be penalized for doing something unknowingly which is not the accepted behavior. Is there any such Code of Conduct?
Approach for problem GAMCOUNT
The problem link is: https://www.codechef.com/problems/GAMCOUNT Approach:
As we know Henry gonna win every game in which possible number of all valid moves is odd. So we calculate total number of valid moves of each of Xi(see NOTE) and sum them. More generally Henry wins if the sum of all these moves is odd ,also problem can be simplified if considering total moves of each number Xi as either even or odd, then just look if there are odd number of odds in initial given Xi set and count these games in answer. - Is this approach is correct? NOTE: we can simply get valid number of moves with Xi by just knowing how many times we can subtract 2^V(X**i) so that resulting number be >=0.
WORKCHEF and POLYEVAL problems in July 16
Can someone explain the solutions for the problems WORKCHEF and POLYEVAL in July16?
Unethical Karma Farming?
I am finally frustrated and have to post this .I have been observing the codechef profile of this months top contributor @laxman94 for today and have found that most of his upvotes are done by his college mates. Over the past 2 days, his classmates @harryi3t (10 upvotes), @nagendra085 (10 upvotes) and @praveen2jan (6 upvotes) have upvoted almost each and every one of his answers in a bid to increase his karma.This is just todays observation and not for the whole month!
I do not know if he is personally involved in this but i feel it is highly unethical and unfair to people like @kuruma who deserves to be Top Contributor as he has genuinely contributed and is not just getting upvotes from his friends. Please do not indulge in such activities and disturb this wonderful forum otherwise the karma system would completely lost its meaning.
Doubt regading NTT
I have a doubt about how devide polynomial in NTT.Specifically in this problem link :https://www.codechef.com/JULY16/problems/POLYEVAL/ In editorial and many solution,they are deviding polynomial into 3 polynomials. I didn't understand why is needed to devide the polynomial into 3 polynomial first and then apply NTT. Can we take whole polynomial into single array and do it's NTT.Please Clarify my doubt.
COOL RANK NAMES TO STARS GIVEN BY CODECHEF
@admin I was wondering adding rank names like newbie, specialist, pupil etc like they have at codeforces for defined range of rating..
if codechef can also do something like that then it would add to already exciting star studded system..
if u all think that it will be a good idea.. then support it !!
top contributor's view req. @vijju123
ALOST - Editorial
PROBLEM LINK
Contributors
Author:Amit Pandey
Tester & Editorialist:Prateek Gupta
DIFFICULTY:
Medium
PREREQUISITES:
Number theory
PROBLEM:
Find any valid array of $N$ integers having $E$ contiguous subarrays with even sum and $O$ contiguous subarrays having odd sum. In case, no such array exists, output "-1" (without quotes).
EXPLANATION
Solution for Subtask 1:
Since, the constraint of $N$ is as small as $15$, it can be easily solved by bit masking. It is a simple realization that each of the $N$ integers will be either odd or of even parity and that is how the count of even sum and odd sum contiguous subarrays will be impacted. Having said that, we can place either an odd or an even integer in each of the $N$ places. This gives rise to iterating over all sets of combinations and finding the valid one if it exists.
for ( mask = 0 to 2^n - 1 ) {
a[0] = 0
even_subarrays = odd_subarrays = 0
for ( j = 0 to n - 1 ) {
if ( bit j is set in mask ) a[j + 1] = 1
else a[j + 1] = 0
}
for ( j = 1 to n ) {
sum = 0
for ( k = j to n ) {
sum += a[k], sum %= 2
if ( sum ) odd_subarrays += 1
else even_subarrays += 1
}
}
if ( even_subarrays == E and odd_subarrays == O ) {
print(array, a)
exit
}
}
print("-1")
The overall complexity for the above approach is $\mathcal{O}(N^2*2^N)$ which is exponential and hence too slow for the rest of the subtasks.
Solution for subtask 2 & 3:
The key to approach the optimized solution is to start from backwards.
Let us denote the $prefixSum_i$ as the sum of first $i$ integers of any valid array to be computed. Now, there are some important observations.
- If you somehow know the number of prefixSums having odd and even parity respectively, you can correspondingly create any valid array provided that total count of oddPrefixSums and evenPrefixSums is $N\ +\ 1$. For eg :- If you have 3 evenPrefixSums and 2 oddPrefixSums, you can create an array $[0,\ 0,\ 1,\ 0]$. The trick is to place an only $1$ after placing $evenPrefixSums\ -\ 1$ zeros. All the remaining prefixSums will obviously be of odd parity. Having said this, the following equation holds true. $$evenPrefixSums\ +\ oddPrefixSums\ =\ N\ +\ 1$$
- How to calculate the number of contiguous subarrays having odd parity? Since, $prefixSum_i\ -\ prefixSum_j$ contributes to sums of contiguous subarrays, both should be of different parity. Hence, number of contiguous subarrays having odd parity will indeed be $C(evenPrefixSums,\ 1)*C(oddPrefixSums,\ 1)$. This gives rise to another equation. $$evenPrefixSums\ *\ oddPrefixSums\ =\ O$$
How to compute the number of evenPrefixSums and oddPrefixSums?
Approach 1
Looking at equation $2$ which is of the form $a*b\ =\ X$, you can go ahead to compute the divisors of $X$ which will satisfy $a\ =\ i$ and $b\ =\ X/i$ provided that $X\ mod\ i\ =\ 0$. If any such pair of $(a,\ b)$ also satisfies the equation $1$ of the form $a\ +\ b\ =\ Y$, we get a valid pair. Find the pseudo code below for the same.
LIM = square_root(odd)
for ( i = 1 to LIM ) {
if ( odd % i == 0 ) {
if ( i + odd/i == n + 1 ) {
odd_prefix_sums = i
even_prefix_sums = odd/i
}
}
}
Approach 2
Alternatively, you can form a quadratic equation and solve it to get the respective values. If you do not find any valid values, output "-1".
Note
Depending upon your implementation, cases having $E$ or $O$ as $0$ might be tricky.
For more details on the implementation of any subtasks, have a look at the tester's solution. If you want to enjoy a good reading about other Number theory algorithms, feel free to visit this blog post.
COMPLEXITY
Overall time complexity for the above approach is $\mathcal{O}(N)$ per each test case.
SOLUTIONS
Setter
Tester's solution to Subtask 1
Tester's solution to Subtask 2 & 3 - Approach 1
Tester's solution to Subtask 2 & 3 - Approach 2
Request for Video Editorials
Finally some VIDEO EDITORIALS
I found this awesome channel :
https://www.youtube.com/channel/UCRPMAqdtSgd0Ipeef7iFsKw
it's a great initiative by @gkcs to give much needed video editorials..
Since he has started a few month back only he needs support of all those who seek video editorials. I am happy to share this link with u all..
You can ask him to upload video editorial on questions across different challenges.. if he finds them worthy enough he will surely make a video on it.
Bug Found on codeChef site?! Even after solving problem, problem is not automatically removed from To-do list!
i added a lot of unsolved problems on to-do list for solving them in future but after solving some of those to-do list problem, it is not removed from to-do list
but before codechef website update it was working fine, after solving problem if it is present in to-do list it gets removed from to-do list
FIBQ - editorial
PROBLEM LINK:
Author:Sunny Aggarwal
Tester:Sergey Kulik
Editorialist:Mugurel Ionut Andreica
DIFFICULTY:
MEDIUM
PREREQUISITES:
Fibonacci Numbers, Segment Trees
PROBLEM:
Given an array A consisting of N elements, Chef asked you to process following two types of queries on this array accurately and efficiently.
- C X Y: Change the value of X-th element of array to Y i.e A[X] = Y.
- Q L R: Compute the function F over the subarray defined by the elements of array A in the range L to R, both inclusive.
The function F(S) is defined as $F(S)=(\sum_{W\subseteq S}{Fibonacci(Sum(W))})\ modulo\ (10^9+7)$, where Sum(W) denotes the sum of all the elements in the sub-multiset W and Fibonacci(Z)=the Z-th Fibonacci number.
The function F applied over a subarray [L,R] of the array A is defined as F(S) where S is the multiset consisting of all the elements from the range [L,R] from the array A (i.e. S={A(L), A(L+1), ..., A(R)}).
EXPLANATION:
We can use the following property of Fibonacci numbers: Fibonacci(A+B)=Fibonacci(A)xFibonacci(B+1)+Fibonacci(A-1)xFibonacci(B).
With this property we can use a segment tree in order to efficiently process updates and queries. For each interval corresponding to a node of the segment tree we will store 3 values:
- sfib = sum of the values Fibonacci(Sum(W)) (modulo $10^9+7$), for all the sub-multisets W
- sfibm1 = sum of the values Fibonacci(Sum(W) - 1) (modulo $10^9+7$), for all the sub-multisets W
- sfibp1 = sum of the values Fibonacci(Sum(W) + 1) (modulo $10^9+7$), for all the sub-multisets W
For the leaves of the segment tree we will initialize these values directly. For a leaf node L corresponding to a position i of the array A we will simply set:
- L.sfib = Fibonacci(A[i])
- L.sfibm1 = Fibonacci(A[i] - 1)
- L.sfibp1 = Fibonacci(A[i] + 1)
Since A[i] can be pretty large, we need an efficient method of computing Fibonacci(A[i]) (and, in general, for computing Fibonacci(Y) for Y as large as $10^9$). There are multiple methods which can be used for computing these values in O(log(Y)) time. Below you can see one such function which uses memoization:
Fibonacci(y) { if (y <= 0) return 0; if (y <= 2) return 1; if (y in fibonacci_cache) { return fibonacci_cache[y]; } int f, b, a; b = y / 2; // integer division a = y - b; f = (Fibonacci(a) * Fibonacci(b + 1) + Fibonacci(a - 1) * Fibonacci(b)) % MOD; fibonacci_cache[y] = f; return f; }
For a non-leaf node node we can use the following function for computing its information based on the information available in its left and right children:
CombineIntervalInfo(left, right, node) { node.sfib = (left.sfib + right.sfib + left.sfib * right.sfibp1 + left.sfibm1 * right.sfib) % MOD; node.sfibm1 = (left.sfibm1 + right.sfibm1 + left.sfib * right.sfib + left.sfibm1 * right.sfibm1) % MOD; node.sfibp1 = (left.sfibp1 + right.sfibp1 + left.sfibp1 * right.sfibp1 + left.sfib * right.sfib) % MOD; }
Processing an update operation X Y can be done as follows in O(log(N)) time. For the leaf node L corresponding to the position X from the array we reinitialize its values:
- L.sfib = Fibonacci(Y)
- L.sfibm1 = Fibonacci(Y - 1)
- L.sfibp1 = Fibonacci(Y + 1)
Then, for every ancestor of the node L, starting from its parent and going up to the root, we recompute its information using the CombineIntervalInfo function.
Handling a query L R can be done as follows, also in O(log(N)) time. We find the O(log(N)) nodes of the segment tree such that the disjoint union of their intervals is equal to the interval [L,R]. Let these nodes be node(1), ..., node(K). If K=1 then the answer is node(K).sfib. Otherwise, we will we compute a tuple ANS containing the same fields sfib, sfibm1 and sfibp1 as the information stored for each segment tree node. We initialize ANS by combining the information from node(1) and node(2). Then, for every $3\leq i\leq K$ we update ANS by combining its information with that of node(i). At the end, the answer is ANS.sfib.
AUTHOR'S, TESTER'S AND EDITORIALIST'S SOLUTIONS:
Author's solution can be found here.
Tester's solution can be found here.
Editorialist's solution can be found here.
How to Use Dynamic Programming in this problem?
Problem-Riya is quite famous for her problem-solving skills in VESIT. When she was facing an interview for placement, a recruiter asked her this question :
Let's consider a triangle of numbers in which a number appears in the first line, two numbers appear in the second line, three in the third line, etc. Develop a program which will compute the largest of the sums of numbers that appear on the paths starting from the top towards the base, so that:
on each path the next number is located on the row below, more precisely either directly below or below and one place to the right. Remember you cannot travel to the number which is below and one place to the left.
Please Explain....
Offline and Online query
what is online and offline query ? And how to convert a offline query into online query ?
Tirth and Badminton Hackerearth problem
Can someone provide the editorial of this hackerearth problem?
March Cook OFF annoucement
Hello everyone!
I’d like you to invite for CodeChef March Cook-Off that will start at 21:30 IST of 19-th March 2017 (check your time zone here) and will last 2.5 hours.
I am the author of problems and editorials, while Errichto is the tester. There are also some other people who helped with translations and miscellaneous issues who I will fill in later.
There is no registration required, anybody with a CodeChef handle can participate. Top participants can win CodeChef laddus (details on the contest page).
You will be provided 5 problems. There's one problem that shows a style of problems I would like to see more in competitions. Let me know what your thoughts are after the contest.
Hope to see you all on the leaderboard!
Abacus'17-OLPC Geek Tree Solution
Hello, the contest Abacus'17 has just finished. Can somebody tell how to solve the problem, Geek Tree.
VCS - Editorial
PROBLEM LINK:
Author:Konstantsin Sokol
Tester:Gedi Zheng
Editorialist:Miguel Oliveira
DIFFICULTY:
Easy.
PREREQUISITES:
Sets.
PROBLEM
Given 2 sets of integers, count the number of integers that appear in both sets and the number of integers between 1 and N that do not appear in either set.
QUICK EXPLANATION
The source files that are both tracked and ignored are the intersection of the two sets given in the input.
The source files that are both untracked and unignored are the numbers in the interval [1, N] that do not appear in either of the given lists.
EXPLANATION
We are given 2 lists of unique integers. We can treat these lists as sets. A set is a collection of distinct items (integers in this case).
Both ignored and tracked source files
The source files that are both tracked and ignored are the integers in the intersection of the 2 given sets.
To calculate the set intersection, the simplest way is to search all integers of the first set in the second set. Since we have only 100 numbers, we could do this search naively with 2 nested loops and a time complexity of O(M*K).
A smarter way is to use a hash table. Note that the numbers are between 1 and 100. We can use an array with 100 positions and mark position i if number i is in the set. This way we can look-up if a number is in a set in O(1) time. Thus, the time complexity of the set intersection is O(M) to build set A, O(K) to build set B and O(M) to check if the items in set A appear in set B. The total time complexity of this set intersection is O(M+K).
Also, you can use set, map, unordered_map in C++ to make a look up table too.
Also, sometimes you can use bitwise operations with bit packing the sets in the integer to int or long data type. Then, you can use bitwise operations (e.g. and, or, xor) to check set intersection. Also, for two sorted arrays a, b, you can use set_intersection in C++ to find number of common elements in both the arrays in linear time.
Both unignored and untracked source files
The number of source files that are both untracked and unignored are the integers between 1 and N that do not appear in set A or B. We can use the same logic and search all numbers between 1 and N in sets A and B. This will take O(N) time if we use hashing or O(N * (M+K)) with the naive search.
Time Complexity
The total time complexities are O(N + M + K) using hashing and O(M * K + N * (M+K)). Both were perfectly fine as the size of the sets are up to 100.
AUTHOR'S AND TESTER'S SOLUTIONS:
Abacus-17-ABCS03- Please Help.
Problem link
My Submissions here
My logic-
Use Array to store how many buildings of a particular height exist.
Use formula n*(n-1) to calculate all possible pairs.
Eg-
Let there be 5 buildings of height one. I had already defined an array "height[1000001]={0}" to store number of buildings of a particular height. So, it will store 5 for 1.
Now, say our array is like- 1 1 1 1 1
I can pair 1 at I=0 with 1 at I=1,2,3,4. Similarly I can pair 1 at I=1 with 1 at 1=0,2,3,4.
So I derive the formula- Number of Pairs = n(n-1) (via mathematical induction)
But its giving me WA. Can someone help? :)