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

Invitation to CodeChef February Long Challenge 2019 sponsored By ShareChat!

$
0
0

Greetings CodeChef community!

Come participate in CodeChef’s February Long challenge sponsored by ShareChat. This is a 10-day contest that invites participants to solve 7 problems and 1 tie-breaker style challenge problem. The contest is open to programmers of all skill levels. The contest problem statements will be available in English, Hindi, Bengali, Russian, Mandarin and Vietnamese.

Participants of Long Challenge also have an opportunity to apply for job openings at ShareChat - India’s fastest growing social network! Visit the contest link to know more.

I hope you will join your fellow programmers and enjoy the contest problems. Joining me on the problem setting panel are:

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: 1st February 2019 (1500 hrs) to 11th February 2019 (1500 hrs). (Indian Standard Time — +5:30 GMT) — Check your timezone.

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

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. First to solve each problem except challenge - 100 laddus. 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 !!


What are karma points?

$
0
0

I am new to this karma system, Can you explain me why is it called so? What has karma points got to do with contribution?

MMC - Editorial

$
0
0

PROBLEM LINK:

Practice
Contest

Author:Michael Nematollahi
Tester:Teja Vardhan Reddy
Editorialist:Michael Nematollahi

PREREQUISITES:

DP

EXPLANATION:

Let's start with defining a few arrays and dp arrays:

1_ $d_0[r][cnt]$ is equal to the minimum time required to eat $cnt$ cheese wedges from the first $(r-1)$ rows and getting to the first column of the $r^{th}$ row.

2_ $d_1[r][cnt]$: similar to $d_0$ except you need to get to the last column of the $r^{th}$ row.

3_ $ans[r][cnt]$ is equal to the minimum time required to eat $cnt$ cheese wedges from the first $r$ rows.

4_ $c_0[1][r][cnt]$ is equal to the minimum time required to start from the first column of the $r^{th}$ row and go to the right and eat $cnt$ of the cheese wedges on the $r^{th}$ row.

5_ $c_0[2][r][cnt]$ is equal to the minimum time required to start from the first column of the $r^{th}$ row and go to the right and eat $cnt$ of the cheese wedges on the $r^{th}$ row and get back to the first column.

6_ $c_1[1][r][cnt]$ is the same as $c_0[1][r][cnt]$ except that it's for the last column.

7_ $c_1[2][r][cnt]$ is the same as $c_0[2][r][cnt]$ except that it's for the last column.

8_ $z[r][cnt]$ is equal to the minimum sum of $cnt$ cheese wedges from row $r$.

9_ $g[r]$ is equal to the number of cheese wedges on row $r$.

Alright, now we're done with definitions. The answer to the problem would be stored in $ans$. So all that's left is to calculate these arrays. Here we go through them one by one and explain how you can calculate them:

1_ $d_0[r][cnt] = \min(\min_{0 \leq i \leq g[r-1]} (d_0[r-1][cnt-i]+1+c_0[2][r-1][i]), \min_{0 \leq i \leq g[r-1]} (d_1[r-1][cnt-i]+m+z[r-1][i])$. If we ignore the rows with no cheese on them, the complexity of this part would be $O(K^2)$.

The first part of the min function above is for the case where we eat $cnt-i$ wedges from the first $r-2$ rows, climb up from the first column and reach row $r-1$, eat $i$ wedges from the $(r-1)^{th}$ row, come back to the first column and then go up.
The second part is for the case where we do the same thing as the first case, except that we climb up to row $r-1$ from the last column.

2_ Similar to 1.

3_ $ans[r][cnt] = \min (ans[r-1][cnt], \min_{1 \leq i \leq g[r]} (d_0[r][cnt - i] + c_0[1][r][i]), \min_{1 \leq i \leq g[r]} (d_1[r][cnt-i] + c_1[1][r][i]))$. If we ignore the rows that don't have any cheese on them, the complexity of this part would be $O(K^2)$.

The first part of the min function above is for the case where we take the wedges from the first $r-1$ rows.
The second part is for the case where we climb up to the $r^{th}$ row from the first column and eat $i$ wedges from the row.
The third part is for the case where we climb up the $r^{th}$ row from the last column and eat $i$ wedges from the row.

4_ The answer depends on the position of the rightmost cheese wedge we eat on this row. Let's loop over this rightmost cheese. Assume that the wedges on row $r$ are numbered from left to right. Assume the rightmost wedge we eat is the $i^{th}$ one. It's easy to see that if we want to eat $cnt$ wedges, it's best to eat the $cnt$ wedges with minimum $t$ amongst these $i$ wedges. This observation gives us the following solution:

Let $i$ be the rightmost cheese wedge we eat. Sort the first $i$ wedges and set $c_0[1][r][cnt] = \min (c_0[1][r][cnt], pt[cnt])$ where $pt$ is the partial sum array over the sorted cheese wedges. Note that since we have the sorted array for the first $i-1$ wedges, the sorting can be done in $O(i)$. Hence, this part of the solution runs in $O(g[r]^2)$, which leads to the overall complexity of $O(K^2)$ over all rows.

5_ Similar to 4.

6_ Similar to 4.

7_ Similar to 5.

8_ To calculate $z[r][cnt]$, we can sort the cheese wedges on row $r$ based on $t$(the time required to eat a wedge). Then, $z[r][cnt]$ would be equal to the sum of the first $cnt$ $t$s. If we ignore the rows that have no cheese on them, this array can be calculated in $O(K \times log(K))$.

The total complexity of this solution is $O(K^2)$. Refer to the setter's solution to see the implementation.

AUTHOR'S AND TESTER'S SOLUTIONS:

Author's solution can be found here.
Tester's solution can be found here.

how to compare more than 2 strings at a time, where the strings are taken from user

$
0
0

how to compare more than 2 strings at a time, where the strings are taken from user

Deleted .

error while taking input in python

$
0
0

I tried to take an input in Python but the compiler showed a runtime error (NZEC error). I have tried many solutions but none seem to work.

This is line on which I am getting an error for problem code HS08TEST

amt,total=map(int,input().split(" "))

What should I do to solve that error

help solving practice test - ANTMAT

Music appplication

$
0
0

I am trying to code an application which will act as a virtual karaoke app. I want to compare the users voice to the original singer voice and i do not know on what basis can i do that, Is this even possible? I want to design this to generate user scores based on how close their pitch reaches to the original singer.


tasks in a subtask

$
0
0

i am solving a problem in which only one subtask in given.in this i am getting AC for 2 tasks and WA for 6 task. someone please help me do solve this problem

Dynamic programming with bit masking.

$
0
0

I am attempting to solve ASSIGN on spoj. It's a simple dp+bitmask problem. I tried solving it recursively but for some reason I keep getting TLE.

Help me figure out if I can further optimize my code to reduce its time complexity or if it can only be solved iteratively.

My solution - my solution

Time complexity - O(Number of test_cases * n * 2^n)

Space Complexity - O(n*2^n + n^2)

Extremely unbalanced short contests

$
0
0

It has been my observation that CodeChef's short contests like monthly Cook-Offs and Lunchtimes have become extremely unbalanced in difficulty level.

The best example I have is of COOK101 (December Cook-Off). Being in Division 1, I could not do a single question from that contest (thankfully I made no submissions). I understand Division 1 is meant to be hard but among 5 questions at least 1 should be on the easier side (greedy algorithms, implementation problems, etc.).

But the true problem occurs with Division 2 (COOK101B). Their first question was easy, and the next 4 questions are common with Division 1. Look at the fall in successful submissions, 2080 to 85. That means for more than 95% of participants, acing the Cook-Off meant doing the easy question fast. That was the end of the contest for them.

And usually also, if there are 5 questions in a Cook-Off or Lunchtime, the last 3 have successful submissions in single digits, compared to >100 submissions for the first two problems.

Now, I understand that having hard questions is good for learning. But still the contests are so unbalanced, it's not fun at all. And Division 2 contests are not appropriate for Division 2 level at all. Division 1 also feels like as if its for rating >= 2100.

I think that many other users might also be disappointed with the current difficulty distributions.

Need clarification of MAGICJAR question

$
0
0

In the second case of MAGICJAR of February Long Challenge ,if the junior chef no.1 had taken 1 jar first then chef 2 would have 3(<4) jars thus failing the session .How is the output justified ?

why i am getting RE (NZEC) error in subtasks one [JAN LUNCHTIME DIV2 WTCH]?

NZEC runtime error in python 3

$
0
0
t = int(input())
l = []

MAX = 100

prime_num = [1] * (MAX + 1)

prime_num[0] = 0
prime_num[1] = 0

for i in range(2,MAX+1):
    if prime_num[i] == 1:
        val = i
        for j in range(2*val, MAX+1 , val):
            prime_num[j] = 0

while t:
    m , n = [int(i) for i in input().split(' ')]
    if m < 2:
        m = 2
    for i in range(m,n+1):
        if prime_num[i] == 1:
            print(i) 
    print('')    
    t -= 1

Adding 2 numbers using doubly linked list in C++

$
0
0

The user is supposed to enter 2 numbers.And then you need to create separate lists for the 2 numbers.Now you need to split the 1st number into the nodes of the list.(for example the user entered a number of 20 digits.then each node should contain atleast 4 digits of the number).Now you need to add these two numbers according the nodes and the carry should be added to the next nodes.It means that the first node of the first list will be added to the first node of the second list and if there is a carry then it will be added to the second nodes.The following result should be stored in a new list following the same pattern of storing 4 digits in one node.


MINVOTE - Editorial

$
0
0

PROBLEM LINK:

Div1, Div2
Practice

Author:Praveen Dhinwa
Tester:Triveni Mahatha
Editorialist:Adarsh Kumar

DIFFICULTY:

Easy-Medium

PREREQUISITES:

Binary search

PROBLEM:

You are given an array $A$ of $N$ positive integers. For all $i$, you need to find number of such $j (j \ne i)$ for which sum of number between $(i,j)$ (both exclusive) is less than or equal to $j^{th}$ element.

EXPLANATION:

I will try to explain the most straight forward solution that is easy to think at first.

We will try to iterate over every minion $j$ and try to find ranges of minion that will get voted by $j^{th}$ minion. For $i^{th}$ minion to get voted from $j^{th}$ one following condition must be satisfied: $A[j] \ge sum(i,j)$.

Let's just solve for the case $i>j$. We can extend this one for the case $ i < j $ too using the similar argument. You can observe that right side of our condition is an increasing function while the left side is a constant. We can use binary search on $i$ to find the break-point now. Now you just need to add 1 to the range $(j+1,\text{last_valid_i})$.

This problem reduces to offline range update and point query at every point. There are many possible ways of doing this. One of them uses DP with $O(n)$ time complexity. I will describe that one in brief.

Say, you are given $q$ updates $(l_i,r_i)$, where in each update you need to add 1 to the range $(l_i,r_i)$. Also, in the end you are required to report all the values of the array. A simple pseudocode for solving this task:

# arrays are initialized with zeroes
A[0...(N+1)]  # Our oiginal array
for i=1...q:
  A[r_i]+=1
  A[l_i-1]-=1
for i=N...1:
  A[i]+=A[i+1] # A[i] now contains the final value at ith position

ALTERNATE SOLUTION

Even a solution that will look like a brute force at first sight is sufficient. Let's take a look at pseudocode:

j = 1 to N:
  i = j+1 to N:
    if sum(i,j) > A[j]:
      break
    ans[i]+=1
  i = j-1 to 1:
    if sum(i,j) > A[j]:
      break
    ans[i]+=1
$ans[i]$ in the above pseudocode stores number of votes received by each minion. Time complexity of the above solution is $O(N.log(MAX))$. Proof is left as an exercise to reader.

Time Complexity:

$O(N.logN)$

AUTHOR'S AND TESTER'S SOLUTIONS

Setter's solution
Tester's solution

GMEDIAN - EDITORIAL

$
0
0

PROBLEM LINK:

Practice
Contest: Division 1
Contest: Division 2

Setter:Teja Vardhan Reddy
Tester:Zhong Ziqian
Editorialist:Taranpreet Singh

DIFFICULTY:

Easy-Medium

PREREQUISITES:

Combinatorics and Factorials. Modular Arithmetic.

PROBLEM:

Given an array $A$ of size $N$, Count the number of ways we can select a subset from $A$ such that the median of selected subset is also present as an element in the subset.

SUPER QUICK EXPLANATION

  • Every subset of odd size has its median present in the subset, so, we can directly add $2^{N-1}$ to answer.
  • For even size subset, The subset is good, if and only if middle two elements are equal.
  • We can fix the left middle element, and for every possible even size of the subset, say $2*X$, try to

EXPLANATION

This problem is probably the best example of how we can use combinatorics to optimize our solution from $O(N^3)$ to $O(N)$ (except sorting).

First of all, let us formulate the definition of median.

If the size of the selected subset is odd, the median is just the middle element of subset after sorting. Since the middle element is present in the subset, all subsets of odd size are valid. It can be easily proven that there are $2^{N-1}$ such subsets. So, we can directly count these and move toward even size subsets.

If the size of the selected subset is even, the median is defined as the average of two middlemost elements after sorting. Now, say we have two middle elements $x$ and $y$, with condition $x \leq y$. Let $z = (x+y)/2$ be the median of sequence. If we write $y = x+d, d \geq 0$, we can see, $z = x+d/2$ and also, $z = y-d/2$. This way, we can see, that the median of a sequence can never be smaller than $x$ and greater than $y$. So, For $z$ to be present in subset, we need either $z = x$ or $z = y$. But, this would imply $d = 0$, Hence leading to the conclusion that

For an even size subset to be valid, the two middlemost elements should be equal. This forms the crux of our solution, and now, we need to count the number of even sized subsets with equal middle elements.

After all this, there are a number of approaches to solving this problem, all of which required us to sort the array $A$ first.

Let us consider a $O(N^3)$ solution first.

Iterate over every pair of equal elements $(i, j)$ such that $A_i = A_j$ and iterate over the size $2*X$ of subset from $X = 1$ to $N$. The number of ways to make the subset of size $X$ with two fixed middle elements is just the product of the number of ways we can select $X-1$ elements from $[1, i-1]$ and $X-1$ elements from $[j+1, N]$.

This solution requires to iterate over every pair $(i,j)$ which takes $O(N^2)$ time and $O(N)$ time per pair, leading to Overall time complexity $O(N^3)$ which shall pass only the first subtask.

For a faster implementation, We will see two implementations, one from setter, and another interesting solution which we can further optimize to $O(N)$ except for sorting.

Setter's Implementation

First of all, see what we were doing in the previous solution.

We were fixing two equal elements and tried to count the number of ways we can make subsets of all sizes. Now, We shall fix only the Left Middle Element (Or Right one, whichever implementation you prefer).

Suppose we fixed the ith element as the left middle element. Now, We will iterate over all sizes $2*X$ and try to include $X-1$ elements from $[1, i-1]$ and $X$ elements from $[i+1, N]$. We need the right middle element to be same as the left middle element. So, When choosing $X$ elements from $[i+1, N]$, we need at least one occurrence of $A[i]$. This is same as subtracting all the ways to select $X$ elements in the range $[i+1, N]$ which do not have $A[i]$ at all. Suppose Number of occurrence of $A[i]$ in range $[i+1, N]$ is $f$, then we can count the number of ways to select $X$ elements from range $[i+1, N]$ such that it contains at least one occurrence of $A[i]$ as

$T$ = Number of ways to select $X$ elements out of $N-i$ elements less Number of ways to select $X$ elements out of $N-i-f$ elements.

We can select $X-1$ elements from $[1, i-1]$ in suppose $U$ ways. Then, Number of ways we can have good subsets with $A[i]$ as the left middle element is $U*T$. Summation of this product for all sizes for all elements gives us the number of good subsets of even size. We can add to it, the number of good odd sized subsets and print the answer.

Alternative Implementation.

For this solution, We will not count good subsets, but subtract bad subsets from total subsets. The total number of subsets is $2^N$ out of which one is empty. Empty subset doesn't contain its median, hence a bad subset. So left with $2^N-1$ subsets.

In this solution, we will count the number of subsets which have the different value of middle elements. For this solution, we iterate over all distinct elements and try to the number of ways to select subsets of all even sizes.

In this solution too, we fix the left middle element and try to count the number of bad subsets.

Suppose we have $cnt$ elements smaller than $A[i]$ and There are total $f$ occurrences of $A[i]$ and we have to make a subset of size $2*X$, Then, we need to select $X$ elements from $cnt+f$ such that it contains atleast 1 occurrence of $A[i]$ and selecting $X$ elements from Elements strictly greater than $A[i]$, THere are $N-cnt-f$ such elements. The required number of ways is the product of both. We can find the summation of this product over all even sizes of subsets for all distinct values.

This solution also has time complexity $O(N^2)$, thus fits the time limit easily.

Hint to O(N) Solution. Note: This optimization is not exactly necessary.

We rely on the same idea as the alternative implementation. We still iterate over all distinct values, but now, we will find a closed form to count bad subsets of all sizes. But the thing is, we try to find a closed form for all sizes.

We see, the summation is like $^nC_x*^mC_x$ for all sizes $x$. Can you find the closed form? Seems like many users actually did, and submitted a Linear time solution during the contest itself.

For those who did not reduce, this is left as an exercise for them.

Looking for something helpful? See Vandermonde's identity here.

Computing Number of ways to select $X$ elements out of $N$ elements

The thing is, we need to calculate N select X efficiently. We know from the definition of Combinations that we can represent them in form of factorials. We can precompute Factorials and their inverses for the division. Then, $^NC_R = N!*inv(R!)*inv((N-R)!)$.

For details, refer this excellent post here.

Time Complexity

Time complexity is $O(N^2)$ per test case for the intended solution.

AUTHOR'S AND TESTER'S SOLUTIONS:

Setter's solution
Tester's solution
Editorialist's solution

Feel free to Share your approach, If it differs. Suggestions are always welcomed. :)

Handle an integer of order 10^37

$
0
0

I was solving a problem where I need to multiply two numbers of order 10^18. So I wanted to know whether there is any way in C++ to handle a number of order 10^37.

INOI 2019 Discussion

Call By Reference and Call By Pointer : Can you Spot the Difference :P

$
0
0

The topic of Confusion among most of the beginners and even mine!

Can Someone PLEASE explain the difference between the two !

THANKS!

Viewing all 39796 articles
Browse latest View live


Latest Images

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