Quantcast
Channel: CodeChef Discuss - latest questions
Viewing all 39796 articles
Browse latest View live

Multiple Choice Questions Related To Testing Knowledge about Time and Space Complexity Of A Program

$
0
0

Hi fellow programmers,

We are trying to create a multiple choice quiz for space and time complexity of the programs related questions. Here are a set of 20 questions we collected. Please feel free to give your answers to these questions. Any feedback about the set of questions. Please also feel propose to any more set of MCQs that you would like to add here, there might be some interesting questions that you might have encountered during the programming and would like to add here :)

Q1.

Average case time complexity of quicksort?

A. $\mathcal{O}(n)$
B. $\mathcal{O}(n \log n)$
C. $\mathcal{O}(n^2)$
D. $\mathcal{O}(n^3)$


Q2.

Worst case time complexity of quicksort?

A. $\mathcal{O}(n)$
B. $\mathcal{O}(n \log n)$
C. $\mathcal{O}(n^2)$
D. $\mathcal{O}(n^3)$


Q3.

Time complexity of binary search?

A. $\mathcal{O}(1)$
B. $\mathcal{O}(\log n)$
C. $\mathcal{O}({(\log n)}^2)$
D. $\mathcal{O}(n)$


Q4.

def f()
    ans = 0
    for i = 1 to n:
        for j = 1 to log(i):
            ans += 1
    print(ans)

Time Complexity of this program:

A. $\mathcal{O}(n)$
B. $\mathcal{O}(n \log n)$
C. $\mathcal{O}(n^2)$
D. $\mathcal{O}(n^3)$


Q5.

def f():
    a = 0
    for i = 1 to n:
        a += i;
    b = 0
    for i = 1 to m:
        b += i;

Time Complexity of this program:

A. $\mathcal{O}(n)$
B. $\mathcal{O}(m)$
C. $\mathcal{O}(n + m)$
D. $\mathcal{O}(n * m)$


Q6.

def f():
    a = 0
    for i = 1 to n:
        a += random.randint();          
        b = 0
        for j = 1 to m:
            b += random.randint();

Time Complexity of this program:

A. $\mathcal{O}(n)$
B. $\mathcal{O}(m)$
C. $\mathcal{O}(n + m)$
D. $\mathcal{O}(n * m)$


Q7.

def f():
    int a[n][n]
    // Finding sum of elements of a matrix that are above or on the diagonal.
    sum = 0
    for i = 1 to n:
        for j = i to n:
            sum += a[i][j]
    print(sum)

Time Complexity of this program:

A. $\mathcal{O}(n)$
B. $\mathcal{O}(n \log n)$
C. $\mathcal{O}(n^2)$
D. $\mathcal{O}(n^3)$


Q8.

def f():
    int a[n][n]
    sum = 0
    // Finding sum of elements of a matrix that are strictly above the diagonal.
    for i = 1 to n:
        for j = i to n:
            sum += a[i][j]
    print(sum)

    for i = 1 to n:
        sum -= a[i][i]

Time Complexity of this program:

A. $\mathcal{O}(n)$
B. $\mathcal{O}(n \log n)$
C. $\mathcal{O}(n^2)$
D. $\mathcal{O}(n^3)$


Q9.

def f():
    ans = 0
    for i = 1 to n:
        for j = n to i:
            ans += (i * j)
    print(ans)

Time Complexity of this program:

A. $\mathcal{O}(n)$
B. $\mathcal{O}(n \log n)$
C. $\mathcal{O}(n^2)$
D. $\mathcal{O}(n^3)$


Q10.

def f():
    int a[N + 1][M + 1][K + 1]
    sum = 0
    for i = 1 to N:
        for j = i to M:
            for k = j to K:
                sum += a[i][j]
    print(sum)

Time Complexity of this program:

A. $\mathcal{O}(N + M + K)$
B. $\mathcal{O}(N * M * K)$
C. $\mathcal{O}(N * M + K)$
D. $\mathcal{O}(N + M * K)$


Q11.

def f(n):
    ans = 0
    while (n > 0):
        ans += n
        n /= 2;
    print(ans)

Time Complexity of this program:

A. $\mathcal{O}(\log n)$
B. $\mathcal{O}(n)$
B. $\mathcal{O}(n \log n)$
C. $\mathcal{O}(n^2)$


Q12.

// Find the sum of digits of a number in its decimal representation.
def f(n):
    ans = 0
    while (n > 0):
        ans += n % 10
        n /= 10;
    print(ans)

Time Complexity of this program:

A. $\mathcal{O}(\log_2 n)$
B. $\mathcal{O}(\log_3 n)$
C. $\mathcal{O}(\log_{10} n)$
D. $\mathcal{O}(n)$


Q13.

def f():
    ans = 0
    for (i = n; i >= 1; i /= 2):
        for j = m to i:
            ans += (i * j)
    print(ans)

Time Complexity of this program:

A. $\mathcal{O}(n + m)$
B. $\mathcal{O}(n * m)$
C. $\mathcal{O}(m \log n)$
D. $\mathcal{O}(n \log m)$


Q14.

def f():
    ans = 0
    for (i = n; i >= 1; i /= 2):
        for (j = 1; j <= m; j *= 2)
            ans += (i * j)
    print(ans)

Time Complexity of this program:

A. $\mathcal{O}(n * m)$
B. $\mathcal{O}(\log m \log n)$
C. $\mathcal{O}(m \log n)$
D. $\mathcal{O}(n \log m)$


Q15.

// Finding gcd of two numbers a, b.
def gcd(a, b):
    if (a < b) swap(a, b)
    if (b == 0) return a;
    else return gcd(b, a % b)

Time Complexity of this program:

Let $n = \max\{a, b\}$

A. $\mathcal{O}(1)$
B. $\mathcal{O}(\log n)$
C. $\mathcal{O}(n)$
D. $\mathcal{O}(n^2$


Q16.

// Binary searching in sorted array for finding whether an element exists or not.
def exists(a, x):
    // Check whether the number x exists in the array a.
    lo = 0, hi = len(a) - 1
    while (lo <= hi):
        mid = (lo + hi) / 2
        if (a[mid] == x): return x;
        else if (a[mid] > x): hi = mid - 1;
        else lo = mid + 1;
    return -1; // Not found.

Time Complexity of this program:

Let $n = len(a)$

A. $\mathcal{O}(1)$
B. $\mathcal{O}(\log n)$
C. $\mathcal{O}(n)$
D. $\mathcal{O}(n^2$


Q17.

// Given a sorted array a, find the number of occurrence of number x in the entire array.

def count_occurrences(a, x, lo, hi):
    if lo > hi: return 0
    mid = (lo + hi) / 2;
    if a[mid] < x: return count_occurrences(a, x, mid + 1, hi)
    if a[mid] > x: return count_occurrences(a, x, lo, mid - 1)
    return 1 + count_occurrences(a, x, lo, mid - 1) + count_occurrences(a, x, mid + 1, hi)

// in the main function, we call it as
count_occurrences(a, x, 0, len(a) - 1)

Time Complexity of this program:

Let $n = len(a)$

A. $\mathcal{O}(1)$
B. $\mathcal{O}(\log n)$
C. $\mathcal{O}(n)$
D. $\mathcal{O}(n^2$


Q18.

// Finding fibonacci numbers.
def f(n):
    if n == 0 or n == 1: return 1
    return f(n - 1) + f(n - 2)

Time Complexity of this program:

A. $\mathcal{O}(\log n)$
B. $\mathcal{O}(n)$
C. $\mathcal{O}(n^2)$
D. $\mathcal{O}(2^n)$


Q19.

Create array memo[n + 1]

// Finding fibonacci numbers with memoization.
def f(n):
    if memo[n] != -1: return memo[n]
    if n == 0 or n == 1: ans = 1
    else: ans = f(n - 1) + f(n - 2)
    memo[n] = ans
    return ans

// In the main function.
Fill the memo array with all values equal to -1.
ans = f(n)

Time Complexity of this program:

A. $\mathcal{O}(\log n)$
B. $\mathcal{O}(n)$
C. $\mathcal{O}(n^2)$
D. $\mathcal{O}(2^n)$


Q20.

def f(a):
    n = len(a)
    j = 0
    for i = 0 to n - 1:
        while (j < n and a[i] < a[j]):
            j += 1

Time Complexity of this program:

A. $\mathcal{O}(\log n)$
B. $\mathcal{O}(n)$
C. $\mathcal{O}(n \log n)$
D. $\mathcal{O}(n^2)$


Q21.

def f():
    ans = 0
    for i = 1 to n:
        for j = i; j <= n; j += i:
            ans += 1
    print(ans)

Time Complexity of this program:

A. $\mathcal{O}(\log n)$
B. $\mathcal{O}(n)$
D. $\mathcal{O}(n \log n)$
D. $\mathcal{O}(n^2)$


Why is my recursive solution of this(link given) spoj problem giving me WA?

$
0
0

I was doing this spoj problem and written tabulation method which got accepted, then I wrote recursive solution but this gave me the wrong solution(WA), Where is my recursive solution is wrong:-

my tabulation solution which got AC.

I want to ask:-

1). What is wrong with my recursive solution.

2). Can we do all dp problem with tabulation and memoization i.e if we can do with memoization than can we do with tabulation and vice versa for every dp problem?

CHEFPAR- Editorial

$
0
0

PROBLEM LINK:

Div1
Div2
Practice

Setter-Misha Chorniy
Tester-Misha Chorniy
Editorialist-Abhishek Pandey

DIFFICULTY:

CHALLENGE

PRE-REQUISITES:

Varying. Challenge problem is usually an application of your skills in competitive programming in general.

PROBLEM:

Given an array $A[]$ of randomly generated sequences, we have to add some integer $D_i$ (need not be distinct) to each array element $A_i$, where $0\le D_i\le K$. Our goal is to maximize $\frac{1}{M}\sum_{i=1}^{M} B_i$ where $B_i=(A_1*A_2...*A_N)\%P_i$

QUICK ANALYSIS:

It seemed that contestants faced problems in getting a good solution. We're concluding that, because, Some of the trivial solutions got too high points than what they should have got. Majority of the contestants simply printed back the input, or used $input+rand()\%K$ &etc.

ANALYSIS:

The first thing I want to say is that, this editorial is incomplete. And it will remain so, until you guys dont contribute! Its impossible to have any hard and fast approach for the challenge problems, and for the editorial to be at its full potential, I want to request you guys to discuss your approach. Benefit in challenge problem is better gained by discussion, seeing flaws of your approach and analyzing other's strengths- and seeing what worked well, and to what degree. Hence, I would request the community to put forward their approach and intuition behind the question :).

As for editorial, I will try to discuss some of the approaches I saw, in both div1 and div2. I hope you people like it :)

1. Div2-

Not even 10 minutes passed from start of contest on the historical date of $6th$ $April,2018$ when Div2 had got the first few accepted solutions. I had a guess in mind, and I,curious as a doomed cat, decided to see what they did and confirm my intuition. And I dont know if it was fate, or destiny, or perhaps something else, but what I saw was an exact picture of what I had in my mind...

cin>> arr[i]; //Take array input. cout<< arr[i];//Print it back.

This approach got something $\approx 85-88$ points. It was $88.7$ when I checked last on $14th$.
Further solutions were also on similar lines. Eg-

cin>>arr[i]; cout<< arr[i]+rand()%k;

This one got $85.8$ points then. Sad luck for that guy :/

cin>>arr[i]; cout<< arr[i]+k;

This solution performed better, and got around$88-89$ points on average.

Some of the better solutions at div2 which got $\ge90$ involved-

  1. Choose one of the primes. Lets call it $P$ Either largest, smallest, middle one or randomly any.
  2. Make array ideal according to that prime, i.e. add $D_i$ so that $(A_1*A_2..*A_N)\%P$ is maximum.
  3. Pray that this solution gets better points.

By roughly around 20-25 submissions, people experimented with what prime to take. Most of them settled on the median prime.

A good number of approaches used simulation and storing array and its result. Eg-

cin>>arr[i]; Store arr[i]+rand()%k;//Store in a vector etc. Compute score for the just stored array. Repeat above 250-400 times. Print the configuration with maximum score

Depending on luck and number of simulation, the above approach fetched $88-94.7$ points. I saw quite a few with $94.7$ points.

Some of the top approaches also use the concept of simulating till just about to time-out. The contestants chosed a distribution (random, or some other) which they simulated for $\approx3.8-3.95$ seconds where they sought to see which choice of $D_i$ is increasing score for a particular $A_i$. When about to time out, they aborted the process and printed the output they got.

2. Div1

The performance of Div1 was kind of similar to Div2 xD. One of the codes which got $91.1$ points at pretest was-

cin>>A[i]; cout<< A[i]+K/2;

Most of the approaches were common- like simulation till best answer, or take $rand()$ values $300-400$ times. Omitting the common approaches, the approaches of top solutions were quite distinct. (However, most of them are hard to decipher due to 300+ lines of code).

Some crucial optimizations were, however, seen. For example, lets say I got some values of $D_1,D_2...D_N$ and calculated the value of $Score=(A_1+D_1)*(A_2+D_2)*...*(A_N+D_N)\%P_i$. The top solutions preferred to change $D_i$ one by one, and re-calculate $Score$ in $O(Log(A_i+D_i))$ by using inverses as - $NewScore=({A_i+D_{old}})^{-1}*Score*(A_i+D_{new})\%P_i$. This method got $95+$ points on pretest.

Some of the good top codes deserve a mention here. These codes are what one can call crisp :)

  1. Python Code by @gorre_morre
  2. C++ by @anoxx_23
  3. C++ by @mihaibunget

As usual, I want to invite everybody (yes, everybody, not just the top scorers) to discuss their approaches so that we can have an engaging and learning insights into various intuitions :)

CHEF VIJJU'S CORNER:

1. The very first solution submitted by tester to test this problem was also simply printing back the input xD. After that as many as $16$ more submissions were made.

2.Lets discuss the setter's approach here. Lets take a random subset (serveral times) of array $P[]$ and multiply the primes in this subset. Lets call their multiple $MUL$. Now, we now that $MUL\%P_i=0$ where $P_i$ is a prime in the subset.Now, our aim is to get the value of $(A_1+D_1)*(A_2+D_2)....*(A_N+D_N)$ closer to (preferably exactly equal to) $MUL-x$. This is because, by applying $\%$ operation for various $P_i$, we will be left with $MUL\%P_i-x\%P_i=(-x)\%P_i$. We can go greedily and try to factorize $MUL-1,MUL-2,...,MUL-x$ (The exact value of $x$ can be experimented upon). The factorization will help us in manipulation of $D_i$ values.

One thing to check is that, $(A_1+K)*(A_2+K)..*(A_N+K)$ must have a value more than $MUL$ else the above scenario may not be possible. We can check this by using $log$ function, i.e. by changing expression $(A_1+K)*(A_2+K)..*(A_N+K)$
to-
$Log(A_1+K)+Log(A_2+K)+...+Log(A_N+K)$.

Similarly, $MUL$ can be expressed as $(Log(P_1) +Log(P_2)+....+Log(P_N))$

You can find good practice problem here

3.There is also a better random generator in C++ known as Mersenne Twister or mt19937. It is available from C++11 and later. Some of the advantages it has over rand() are that-

  • mt19937 has much longer period than that of rand. This means that it will take its random sequence much longer to repeat itself again.
  • It much better statistical behavior.
  • Several different random number generator engines can be initiated simultaneously with different seed, compared with the single “global” seed srand() provides

GOODPREF -Editorial

$
0
0

PROBLEM LINK:

Div1
Div2
Practice

Setter-Praveen Dhinwa
Tester-Misha Chorniy
Editorialist-Abhishek Pandey

DIFFICULTY:

EASY

PRE-REQUISITES:

Strings, Basic Math, Simulation.

PROBLEM:

Given a string $S$ , with only characters being $'a'$ and $'b$ $'$, find number of good prefixes in the string $T$ which is made by concatenating $N$ copies of $S$, i.e.-

$T = \underbrace{S + S + \dots + S}_{N\text{ times}}$

A prefix is a good prefix if and only if number of occurrences of $'a'$ is strictly greater than the number of occurrences of $'b'$.

QUICK EXPLANATION:

We make cases for this problem. If frequency of $'a'$ is equal to frequency of $'b'$ in string, then calculating answer is simple- for we can simply use $Ans=G(S)*N$ where $G(S)$ denotes number of Good Prefixes in $S$.

If frequency of $'a'$ is more than to frequency of $'b'$ , we notice that after at most $1000$ appendations, each new appendation will contribute maximum possible value only, which is $K$ ($K$=Length of string) .

Similarly we can see that if frequency of $'a'$ is less than to frequency of $'b'$, after $1000$ appendations, each new appendation will contribute $0$ to answer.

So if frequency of character $'a'$ is not equal to that of $'b'$, we make a string $T$ by appending $S$ to it $1000$ times, and then use mathematical formulas to find contribution of every other appendation.

EXPLANATION:

As usual, we will be discussing Editorialist's and tester's approach. Editorialist's approach is based on same lines as that of setter- and hence, will form main part of editorial. We will discuss better optimized solutions (tester's approach) and common mistakes of contestants at the bonus part of editorial :)

Before that, please first get acquainted with the notations I will be using.

  • $G(S)$= Number of Good Prefix in string $S$
  • $freq(a,S)$= Frequency of character 'a' in string $S$.
  • $freq(b,S)$= Frequency of character 'b' in string $S$.
  • ${S}^{p}$= String formed by concatenation $S$ $p$ times.

1. Editorialist's/Setter's Solution-

If you want, you can scroll down to refer to my solution side by side while reading the editorial.

The first thing to note are the constraints. $1\le N \le {10}^{9}$ while $1\le |S| \le {10}^{3}$.

We start by calculating number of Good Prefixes in $S$ , i.e. $G(S)$. Along with that, we also find $freq(a,S)$ and $freq(b,S)$. Now we make cases-

a. When $freq(a,S) == freq(b,S)$-

In this case, there is no affect of previous appendations of $S$ on incoming/new appendations of $S$. In other words, when $freq(a,S) \neq freq(b,S)$, then there is some change in value of $(freq(a,S)-freq(b,S))$ after each appendation of $S$ to $T$, which affect the contribution of any future appendation of $S$ towards $G(T)$. In most basic words, it means that since $freq(a,S) \neq freq(b,S)$, then number of $'b'$ in $T$ will not increase at same rate as number of $'a'$ at each appendation, which will affect contribution of next appendation to the final answer.

This is not the case when $freq(a,S) == freq(b,S)$. Hence, each appendation of $S$ will contribute $G(S)$ towards the final answer. There are $N$ appendations of $S$ and we already found $G(S)$ by simple looping earlier. Hence, for this case, $Answer=G(S)*N$

b. When $freq(a,S) < freq(b,S)$-

In this case, because $ freq(a,S) < freq(b,S)$, each new appendation of $S$ to $T$ will decrease $(freq(a,S)-freq(b,S))$. In other words, the number of $'b'$ in $T$ will increase more quickly than number of $'a'$, which will reduce the contribution of every future appendation of $S$ towards final answer.

We see that, after every appendation, the contribution of next appendation must reduce by $at$ $least$ $1$ , ($\because freq(a,S) < freq(b,S)$). Remember that length of $S$ is only upto $1000$. This simply means, that after at most $1000$ appendations, the contribution of incoming appendation reduces to $0$. (In fact, $1000$ is a loose upper bound, we can go closer to the real value, but for sake of explaining lets take it $1000$ right now :) )

So, we form the string $T$ by appending string $S$ $min(N,1000)$ times as-

$T = \underbrace{S + S + \dots + S}_{min(N,1000)\text{ times}}$

and our answer is equal to $G(T)$ as any future appendation has $0$ contribution towards final answer.

$Answer=G(T)$

b. When $freq(a,S) > freq(b,S)$-

Clearly, each new appendation of $S$ to $T$ will increase $(freq(a,S)-freq(b,S))$. Thus, the number of $'a'$ in $T$ will increase more quickly than number of $'b'$, which will increase the contribution of every future appendation of $S$ towards final answer.

The maximum possible contribution of an appendation to our answer can be $|S|$, i.e. the length of string $S$. Again, as seen above, after $at$ $most$ $1000$ appendations, the next appendation will reach its maximum contribution. Hence, we do same as above, we form $T$ by appending string $S$ $min(N,1000)$ times as-

$T = \underbrace{S + S + \dots + S}_{min(N,1000)\text{ times}}$

Now each future appendation will have maximum contribution, equal to $|S|$. Hence, our answer will be-

$Answer=G(T)+|S|*max(0,N-1000)$

Now, what is the time complexity? How many of you would agree if I said-

$Time$ $Complexity=$ $O(min(N,1000)*|S|) \equiv O(1000*|S|) \equiv O(|S|)$

Is it correct?
.
.
.
(PS: The above time complexity is a lie :) )

SOLUTION:

Setter
Tester - $O(|S|Log(|S|))$
Tester - $O(|S|)$
Editorialist - $O(???)$

CHEF VIJJU'S CORNER:

1.Most of the contestants did a good job in framing the simulation part correctly, checking for overflows, and coming at right expressions. But they $still$ got a $TLE$. And let me reveal the culprit here! It was-

$T=S+T$

Yes, thats right! This expression should had been $T+=S$. The $+$ operator takes time comparable to $O(|S|+|T|)$ while the $+=$ symbol takes time comparable to $O(|S|)$. Treat it like "Creating a new array with characters of $T$ and $S$ $v/s$ Adding $|S|$ characters to back of char-array $T$ ." For reference, have a look at this solution. Replace the $+$ operator with $+=$ and see the time diffference!

2. Tester's $O(|S|log(|S|))$ solution is discussed here. When $freq(a,S)> freq(b,S)$, what he does is, keep a track of $freq(a,S)- freq(b,S)$ at various points of the string, and then sort them. Now, we keep a variable $curr$ which stores the count excessive $'a$ $'$ we get after each appendation. He uses this to check at most how many prefixes more he can get by the condition $have[ptr - 1] + cur > 0$. Depending on that, he adds $ans += (int)have.size() - ptr;$ to the answer. In other words, tries to go checks the least value in of $freq(a,S)- freq(b,S)$ which $curr$, number of excessive $'a'$ $s$ can afford, and adds to answer accordingly. Same approach used for $freq(a,S)< freq(b,S)$ case, which is left for the readers to work out :).

3. Testers $O(|S|)$ solution is a bit similar. Firstly, Misha eliminated sorting, and replaced it with a cumulative summation'd prefix array. Meaning, he stores the frequency of each value of $freq(a,S)- freq(b,S)$ and does a cumulative sum of that array. Again, $curr$ holds $freq(a,S)- freq(b,S)$. Now, his expression is $ans += cnt[2 * SHIFT - 1] - cnt[SHIFT - cur];$, meaning "Max possible contribution - Contributions which cannot be afforded due to low curr". The solution is same in essence to the first one, but he cleverly got rid of sorting, leading to a much simpler $O(|S|)$ approach.

4. Now you have seen two optimized approaches, I will have a question for you. Have a detailed look at the time complexity I mentioned. No doubt, my solution does ,plain and simple, have a complexity of at least $O(min(N,1000)*|S|)$ , but dont you get something fishy? There is a, very common, and intentional error involved here :). Note that, there is no sense in the $"1000"$ I kept in $O(min(N,1000)*|S|)$. Ask yourself, what is this $1000?$ It is nothing but to denote $|S|$!! Hence, the real complexity would be $O(min(N,|S|)*|S|) \equiv O({|S|}^{2})$. Dont be like this editorialist when computing time complexity xD.

5. The string in this question was made up of only $2$ characters. Replace $'a'$ and $'b'$ with $1$ and $0$ and you have a binary string. Some questions on similar topics-

Not Able to Answer any Question on codechef discuss forum ? why? plz help

$
0
0

I am asking this Question on Behalf of my friend anju98 because she have only 1 karma point(due to strict rules of codechef she is not able to ask her doubts)..
other than that, Main problem is-->

She is not able to answer any Question on codechef discuss..

alt text

she is getting "reCapcha V1 is shutDown" .. thats why and not able to answer any question

but then why i m not getting such issues ? Any solution to this problem.. help

Due to only 1 reputation points(1 point is given to every new user in gift by codechef ), she is not able to ask any of her doubt of codechef problems.. So plz tell me How can she Earn enough karma point to ask AnyQuestion..

due to her low karma points,

  1. she can not ask question(doubts)
  2. she can not upvote
  3. she can not downvote
  4. now she thought she will help others begginers by writing on answer and may earn karma points to become elligible for asking her own doubts, but due to this reCaptcha error, she is not able to ask any question

i know, a seperate thread is availble to ask question. who dont have enought karma points, but beginner is unfamiliar initially that a seperate thread is going on to ask doubts..
Thanks In Advance

Invitation to CodeChef April Lunchtime 2018!

$
0
0

Hello Fellow Coders!

The month of April held a special significance in the programming world, with the ACM-ICPC World Finals crowning a new world champion. So how about you cap off this brilliant month with Chef’s next contest, the April Lunchtime 2018 and join your fellow programmers and enjoy the contest problems.

Joining me on the problem setting panel are:

  • Problem Setters, Editorialists and Russian Translators: gainullinildar (Ildar Gainullin), altruist_ (Denis Anischenko)
  • Problem Tester: kingofnumbers (Hasan Jaddouh)
  • Statement Verifier: xellos0 (Jakub Safin)
  • Mandarin Translator: huzecong (Hu Zecong)
  • Vietnamese Translator: VNOI Team

I hope you will enjoy solving them. Please give your feedback on the problem set in the comments below, after the contest.

Contest Details:

Time: 28th April 2018 (1930 hrs) to 28th April 2018 (2230 hrs). (Indian Standard Time — +5:30 GMT) — Check your timezone.

Contest link:https://www.codechef.com/LTIME59

Registration: You just need to have a CodeChef handle to participate. For all those, who are interested and do not have a CodeChef handle, are requested to register in order to participate.

Prizes: Top 10 performers in Global and Indian category will get CodeChef laddus, with which the winners can claim cool CodeChef goodies. Know more here: https://www.codechef.com/laddu. (For those who have not yet got their previous winning, please send an email to winners@codechef.com)

Good Luck!
Hope to see you participating!!
Happy Programming!!

[LOCAPR18] Unofficial Rank List.

$
0
0

@admin Can we have an official rank list. Which includes people from div1 also. So that we can see our standings.

And One more hypothetical situation - If a person moves from div2 to div1 after change in ratings of contest. Because LTIME59 will over before end of this contest. Then this person rating will be calculated according to div2 or div1 ??

(a/b)%c when c is a prime number

$
0
0

how to calculate (a/b)%c when c is a prime number?


Help needed in getting A/C in frequent values problem from spoj?

EURON - Editorial

$
0
0

PROBLEM LINK:

Euron Problem

Practice
Contest

Author:Bhaumik Choksi
Tester:Dipen Ved
Editorialist:Bhaumik Choksi

Under KJSCE CODECELL

DIFFICULTY:

EASY-MEDIUM.

PREREQUISITES:

Divide and Conquer

PROBLEM:

Euron loves to arrange things in order. Euron sticks to his “Golden rule” that every set of numbers must be in ascending order. Unfortunately, that is not always the case. Euron defines a “violation” as a situation when a smaller number comes after a larger number in the set, which violates the ascending order. Given a set of integers, Euron needs to find out the total number of such violations.

QUICK EXPLANATION:

The problem can be solved by using a modified Merge-Sort algorithm, that counts the number of inversions while merging the sub-arrays, in addition to sorting the array.

EXPLANATION:

The merge-sort algorithm involves successively dividing an array into sub-arrays and merging them in a bottom-up manner, while sorting elements of each sub-array into place in the new combined array. To solve this problem, we modify this algorithm to count the number of inversions at every merge step. Since sub-arrays are assumed to be already sorted, no inversion exist before the merge operation. The sum total of the inversions at each merge step gives the total number of inversion in the array.

//Modified Merge method.
static long merge(int[] arr, int[] left, int[] right) {
int i = 0, j = 0;
long count = 0; //Initialize
while (i &lt; left.length || j &lt; right.length) {  
    //Loop till all elements in both subarrays are taken
    if (i == left.length) {
        arr[i+j] = right[j];
        j++; //Left subarray was fully traversed
    } else if (j == right.length) {
        arr[i+j] = left[i];
        i++; //Right subarray was fully traversed
    } else if (left[i] &lt;= right[j]) {
        arr[i+j] = left[i];
        i++; //Left element is less than Right element
        //Hence, no inversion. No count increment.               
    } else {
        arr[i+j] = right[j];
        count += left.length-i;
        //Increment count with the number of inversions
        j++;
    }
}
return count; }

AUTHOR'S AND TESTER'S SOLUTIONS:

Solution can be found here.

Why Pushkar Mishra's profile is suspended?

$
0
0

May I know why Pushkar Mishra's account is suspended? Is suspension an automated process, or is it done manually? Is it possible for me to suspend my account for a desired time period and then re-activate it later on?

Wrong judgement for GCDDIV in April Lunchtime

$
0
0

Question is related to April Lunchtime 2018.

The problem code is GCDDIV

I found two submissions which got 100 points One is https://www.codechef.com/viewsolution/18380850

and the second is https://www.codechef.com/viewsolution/18375921

The first submission got 100 points event thought It does not deserve 100 points because that fails for the test cases like

1

3 6

101 10201 1030301

For this solution 18380850 gives its answer as "YES" but the right answer is "NO" solution 18375921 gives correct answer as "NO" for the above test case.

Now I want this to be looked upon by the codechef and take some fair actions regarding this.

Wrong judgement for GCDDIV in April Lunchtime

$
0
0

Question is related to April Lunchtime 2018

The problem code is GCDDIV I found two submissions which got 100 points One is

https://www.codechef.com/viewsolution/18380850

and the second is

https://www.codechef.com/viewsolution/18375921

The first submission got 100 points event thought It does not deserve 100 points because that fails for the test cases like

1

3 6

101 10201 1030301

For this the solution 18380850 gives its answer as "YES"

but the right answer is "NO" solution 18375921 gives correct answer as "NO" for the above test case

Now I want this to be looked upon by the codechef.

Help in solving Running Median Again from SPOJ

Help in question TREE3: April LunchTime


A good way to learn algorithms?

$
0
0

I am able to solve only 3 or 4 problems in long contests. I don't know much algorithms, so it is hard for me to solve more problems.I have solved many easy problems on codechef. Now, solving medium level problems on codechef for sometime, and them hard ones, a good way to learn algorithms or should i learn an algorithm and then do questions related to it? or something else?

How to begin preparing algorithms?

$
0
0

I am in 2nd year of CS but in our curriculum we have algorithms in 3rd year and data structures currently. But I wish to prepare them now as I have much time which I don't feel would be there in 3rd year.

So what are some good resources for preparation?

How to become a good coder with good problem solving skills??

$
0
0

I want to learn coding, can anyone please suggest me, from where can I study for the same?

Needed help in learning modular arithmatic

$
0
0

I have trying to learn modular arithmatic by solving various problems such as power series sum with modulo and other similar kind of problems involving algebra,combinatorics and modular arithmatc,but getting stuck in every next problem i have been solving.Can someone suggest is it the right way to proceed. If it is then suggest some problems to solve in sequential order. If not then please suggest some learning resource. Thanks in adv!.

[Unofficial Editorial] PALLIND from LOCAPR18

$
0
0

Note:This is my first editorial and i am still a noob in first year of my college so please forgive me for any mistakes.Also suggestions are welcome.

Contest link

LOCAPR18

Problem:

PALLIND

PRE-REQUISITES:

Basic Combinatronics, Basic Probability, Inverse Modulo, Fermat's Little Theorem, Fast exponentiation

PROBLEM:

Bob has a string (S), which is initially empty. In one operation, he can select a lowercase character (a-z) uniformly at random and appends it to S. He then evaluates whether S is a palindrome or not; if it is, he gets 1 point.

Given that Bob performs N operations in total, find the expected no. of points Bob gets. It is guaranteed that the answer can always be represented as a rational number a/b

EXPLANATION:


  • WE WILL DISCUSS THE SOLUTION WHICH FETCHES 100 POINTS

What we basically have to do is find number of palindromic strings in a string of size N. What we find is that if N is odd, number of such strings is 26^((N/2)+1) and 26^(N/2) if even.Let this be P.

Also number of all possible strings is 26^N.

Next thing is for a particular N expected value of palindromic string is P/((26)^N).

Next observation is that while constructing a string of size N we also have to take into consideration all string of size K (1<=K<=N).

Thus we get a series of the form

1+ 1/26 +1/26 + 1/(26^2)+ 1/(26^2) + 1/(26^3) + 1/(26^3) + 1/(26^4) + 1/(26^4) .... upto N terms.

The next task is to represent it in the form a/b. To do this we first take a look at b

After adding the terms we find b is 26^X where X is higest power of 26 in the series. Similarly we find a is something like

 26^x +2.(26^(x-1))+....+(J).1

Where J is 1 if n is even else 2.

This can be represented as a sum of G.P. series with some constraints. Use little fermat's along with fast exponentiation here in calculating the series sum.

Now we are left with (a/b)% 1000000007.

This can be calculated easily using little fermats theorem.

Solution:

Github link

Codechef

Viewing all 39796 articles
Browse latest View live


<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>