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

Why hasn't the rating been updated for july lunchtime

$
0
0

I gave the july lunchtime 2018 as div 2 contestant. My score was 150 but still there have not been an update of my ratings for July Lunchtime.


Incorrect Answer - first time submission

$
0
0

Hi, This is my first submission so I guess there's a learning curve for submitting...

I'm not sure what the criteria are, but below was written "locally" and tested ok (python3).

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

When I submit it I get "Incorrect Answer" and when I try to run it here in the Code,Complile section it gives me an EOF error...

thanks for your help!

August Challenge - SPELLBOB Whats wrong in my code?

$
0
0
    #include "iostream"
    #include "string.h"
    using namespace std;

    int main() {
            string s1, s2;
            int t;
            cin >> t;
            while(t--) {
                cin >> s1 >> s2;
                    if(s1[0] == 'b' || s2[0] == 'b') {
                            if(s1[1] == 'b' || s2[1] == 'b') {
                                    if(s1[2] == 'o' || s2[2] == 'o')
                                        cout << "yes" << endl;
                                    else
                                        cout << "no" << endl;

                            }

                            else if(s1[1] == 'o' || s2[1] == 'o') {
                                if(s1[2] == 'b' || s2[2] == 'b')
                                    cout << "yes" << endl;
                                else
                                    cout << "no" << endl;


                            }
                            else
                                cout << "no" << endl;


                        }


                    else if(s1[0] == 'o' || s2[0] == 'o') {
                            if(s1[1] == 'b' || s2[1] == 'b') {
                                if(s1[2] == 'b' || s2[2] == 'b')
                                    cout << "yes" << endl;
                                else
                                    cout << "no" << endl;

                            }
                            else
                                cout << "no" << endl;


                        }

                    else
                        cout << "no" << endl;
                }
                return 0;

        } 

Wrong Problem in the Contest 'Coding Hours'

$
0
0

In the contest COHR2018, the problem 'Chef And His Algorithm' is probably wrong.

The problem asks us to compute the minimum length of a string which consists of all the permutations of the given string, if the latter has all unique characters. This problem is the same as the Minimal Superpermutation Problem, and it stands unsolved for $n > 5$ as of now.

The answer to the problem was conjectured to be the following value by D. Ashlock and J. Tillotson: $$\sum_{i = 1}^n i!$$ The conjecture has been verified for $n \leqslant 5$, but it was later proven wrong for $n \geqslant 6$. The value of the above sum for $n = 6$ is $873$, but Robin Houston provided a counterexample, which was a superpermutation of length $872$, one less than the conjectured length, to disprove the conjecture. This paper explains the problem and the conjecture, and proof that the conjecture is wrong, in detail.

Quoting from the paper:
The minimal length is still unknown for $n ≥ 6$, but we can show that for all $n ≥ 6$ it is strictly less than the conjectured length $\sum_{i = 1}^n i!$

In the contest, what people have done for this problem is, they have printed the conjectured length for $n \leqslant 5$, and one less than the conjectured length for $n > 5$. However, "strictly less than" the conjectured length does not mean one less than the conjectured length.

So I request @admin to do something about it. Probably remove the problem from CodeChef and its submissions, if possible.

TSECAU05 - Editorial

$
0
0

Problem Link:

Practice

Contest

Author:tseccodecell

DIFFICULTY:

Easy-Medium

PREREQUISITES:

Traversing array, Binary Search

PROBLEM:

Given $N$ values, we have to apply rearrangements by taking $3$ from a value, and give $2$ to any other value such that in the end the minimum value of the array is maximum possible

QUICK EXPLANATION:

Make a function which tells if a given speed is possible or not for Barry by taking speeds from those people who have more than required, and giving to those who have less than required. Apply Binary Search on this function to find answer in less time.

EXPLANATION:

Many solutions are possible for smaller subtask but only one will pass the time limit for all cases.

Our main aim is to apply rearrangement so that the minimum value in the array is maximum possible.

For this we have to give values from a higher element value to lower element value. but how much?

Also, if a value is given to an element, it should not be rearranged as that will not result in a better solution than what we already have.

The expected solution is as follows:

Suppose we have to find whether speed $X$ is possible after applying rearrangements.

For this we iterate the array of person speed limits from beginning to end.

if any value is more than $X$ then we can take $y = \lfloor\frac{value - x}{3}\rfloor$ units and give $2*y$ to other speed limit.

if any value is less than $X$, and $z = \lceil\frac{x - value}{2}\rceil$, then we need $2*z$ units of speed for that value.

After traversing the array and calculating values $y$ and $z$ for each array value, in the end if we get $y >= z$ (speed which we can give >= speed required), then speed of $X$ is possible for the array.

Consider a function $f(x)$ which returns true or false depending on whether $X$ speed value is possible for Barry after optimal rearrangements are applied.

From the above discussion we can see that $f(x)$ will run in $O(n)$ time.

y = 0, z = 0 
for i = 0 to N :
    if(value[i] > x) y += floor( (value[i] - x) / 3 )
    else z += ceil( (x - value[i]) / 2 )

If a speed of $X$ is possible, then all speeds $< X$ are also possible and if speed of $X$ is not possible, then no speed greater than $X$ is possible.

Using this fact, we can iterate the speed from 1 to 'threshold for barry' and find the maximum speed possible for Barry. Since for subtask 1, $S_i <= 10$, this will pass the time limit for subtask 1. $(10*O(n))$

Using the above observations, we can see that Binary Search can be applied on $f(x)$ to find maximum value in less time.

Using $left = 0$ and $right = threshold + 1$, we can apply Binary Search on $f(x)$ to find maximum possible $X$.

left = 0, right = threshold + 1

// iterative binary search
while(right > left+1) :
    mid = (left + right) / 2
    if(f(mid) is true) left = mid
    else right = mid

In the end, the value in $left$ variable will be the answer

Time Complexity : $O(n*log(threshold))$

SOLUTION:

https://ideone.com/OPhbnT

regarding augus tchallenge and its compilation

$
0
0

i have attempted the mgcd question in august challenge.and when i tried to submit the answer ,i have been getting it as incorrect.when i tried to verify it on other compilers the answers matched with the example cases .now i dont know why i have getting is as wrong answer.can u explain me?

BRNDG - Editorials

$
0
0

PROBLEM LINK:

Practice

Contest

Author:rahul_ojha_07

DIFFICULTY:

CAKEWALK

PREREQUISITES:

Binary numbers,Bit Manipulation

PROBLEM:

Given the total number of biscuits Bruno ate, find the number of days he was active?

QUICK EXPLANATION:

Calculate the Binar form of the given Number and count the number of 1's

EXPLANATION:

Since for each day the biscuits doubles up as the previous day with 1 biscuits on the first day, starting from i=0, the number of biscuits Bruno supposed to have on ith day is 2i

. Only the days he was active he got biscuits. So adding the power of 2 on each day he was active, gives the total number of biscuits, i.e., N. Hence, number of 1 in the binary representation of N is the number of days he was active.

Author's solution can be found here.

CHFSQN - Editorial

$
0
0

PROBLEM LINK:

Practice

Contest

Author:rahul_ojha_07

DIFFICULTY: EASY

PREREQUISITES: Simple Math

PROBLEM: Given the Sequence a1, a2, ..., an and Defining the strength of the sequence to be |a1 - a2| + |a2 - a3| + ... + |an-1 - an| + |an - a1| find the maximum strength by rearranging the sequence.

QUICK EXPLANATION: Sort the sequence then arrange it as an ,an ,a2 ,an-2... a(n/2),a(n/2)-1 this is the new sequence through which we can find the maximum strenth of the sequence.

EXPLANATION:

First, sort the numbers. Now, a1 < a2 < a3, ... < an.

Then arrange the numbers like the following:

a1, an, a2, an-1, .... a(n/2), a(n/2+1)

It should not be hard to see that this arrangement produces the optimal answer, as all of a1, a2, ... an/2 are subtracted twice, while all of a(n/2)+1, ... an are added twice. In addition, one nice property of the above sequence is that the elements of the constructed sequence alternate between increasing and decreasing. So, by calculating the strength by above sequence we can find the maximum possible strength

Author's solution can be found here.


unable to find an error

$
0
0

Please explain me the error in my c code. The problem was to find the roots of the quadratic equation after allowing the user to enter the values of a, b and c.

#include <stdio.h>
#include <stdlib.h>
#include <math.h>

int main() {

int a, b, c, x, y;
printf ("enter the value of a \n");
scanf (" %d", &a);

printf ("enter the value of b \n");
scanf (" %d", &b);

printf ("enter the value of c \n");
scanf (" %d", &c);

x = (  sqrt(b*b - 4*a*c) -b )/ (2*a);
y = ( b + sqrt(b*b - 4*a*c) * (-1) )/ (2*a);

printf (" %d \n %d", x,y);


return 0;

}

FICE - Editorial

$
0
0

Problem Links

Practice
Contest

Author: Ankur Goel

Tester: Ankur Goel

Editorialist: Ankur Goel

Difficulty

Medium

Prerequisites

Fibonacci Series, Recursion

Problem

The King wants to select N common folks as solider from the Fire and Ice island in order to win the battle. He can only select odd number of people from land of fire or ice only in one go.He cannot select bunch of people consecutively from either territory.Find the number of ways the mad king could select his army.

Note :The number of people in each territory is infinite.

Quick Explanation

Ans= 2*Fibo(N)%M

Explanation

The Problem is simply based on the Fibonacci Series. Let 0 denotes the people from Land of Ice and 1 denotes the people from Land of Fire then, the number of binary strings of length n without an even number of consecutive 0s or 1s is 2Fn. For example, out of the 16 binary strings of length 4, there are 2F4 = 6 without an even number of consecutive 0s or 1s – they are 0001, 0111, 0101, 1000, 1010, 1110.

Find the Fibonacci using O(Log N) Algorithm and take the modulo M at every step, otherwise your algo may fail due to Time Complexity or due to the Overflow of Fibonacci Number beyond the range of even 64 bit int.

Author's Solution

Author's solution can be found here

Related Problems

Ankur and Coins

"I want to ask a question" - Ask them all here!

$
0
0

Please only ask a question here if you don't have sufficient karma points to post a question

Hello guys,

As you all are aware, many times there are genuine users who wish to ask a question, but dont have karma for it. They then, have to resort to means of either asking their question in other threads, or request someone to upvote them so they can ask a question.

As we have seen last month, some fake accounts were used in malpractices in discuss, and they also gained karma by "I want to ask a question" thing. Hence, now it is becoming increasingly difficult to differentiate between genuine users, and fake users.

This thread is my solution for it. Any user, with less than 3 karma who wants to ask a question is requested to ask it here, and if the question is upto standards (meaning it needs more than those "one line answers") then we (at least I ) will convert your "answer" here into a question.

In short, post your question here, and if its a good genuine question, it will get posted as a separate question by using "Convert to question" feature.

In case your question required only 1 line answers or such, we would comment it on your question.

You guys are requested to direct any such guy (who wants to ask a question) here and not "free-upvote" them, so that we can avoid any duplicate/irrelevant questions (as questions posted here would be verified first, before converting to question).

Note to @admin - We will require your active participation and help in events related to deletion of any spam content here. Also, since only admins are ones who could delete answers, it is requested that you keep an eye out on this thread.

With Regards

Vijju123

PLYNUM – Editorial

$
0
0

PROBLEM LINK:

Practice
Contest

Author:Arkapravo Ghosh
Tester:Arkapravo Ghosh
Editorialist:Arkapravo Ghosh

DIFFICULTY:

EASY-MEDIUM

PREREQUISITES:

Graph, BFS

PROBLEM:

Given a number A, you have to reach another number B using the minimum number of operations, given you can only perform the following operations:-
1) Decrement the current number by 1
2) Increment the current number by 3
3) Multiply the current number by 2

QUICK EXPLANATION:

We can simply consider this as a graph problem. We can consider A as a node in a graph and start a BFS from that node till we reach B. Here each node will have three outgoing edges.

EXPLANATION:

We can consider this problem as a graph problem. Here each number will represent a node in the graph. Each node will have three outgoing edges. We can first start BFS from node A till we reach node B. The three nodes outgoing nodes from a node numbered N will be N-1, N+3 and N*2. But the graph now has infinite number of nodes. However we can limit the range of the node numbering with a simple observation that we don’t need to go below 0 and above B + (B-A)/3. In the worst case we can reach B from A in (B-A)/3 steps, since we can increment by 3 in each step. Again from B + (B-A)/3, we can reach B in (B-A)/3 steps decrementing by 1 each time, since it is the only way of decrementing a number. So, going upto B + (B-A)/3 from A and then decrementing by 1 for (B-A)/3 times is as good as incrementing A to B in (B-A)/3 steps. So we can limit the range of nodes from 0 to B+(B-A)/3.

If B is less than A, then decrementing by 1 in each step is the only way to reach B from A. So it will take (A-B) steps.

The time complexity of this algorithm is proportional to the number of vertices and edges in the graph. In the worst case the no of vertices and edges is (B + (B-A)/3) * 3 = O(B), thus worst case time for BFS will be O(B) i.e. linear time.

AUTHOR'S AND EDITORIALIST'S SOLUTIONS:

Author's and editorialist’s solution can be found here.

KNGPRB – Editorial

$
0
0

PROBLEM LINK:

Practice
Contest

Author:Arkapravo Ghosh
Tester:Arkapravo Ghosh
Editorialist:Arkapravo Ghosh

DIFFICULTY:

MEDIUM

PREREQUISITES:

Graph, Dijkstra, Implementation

PROBLEM:

Given a directed graph, you need to find the minimum no of edges to be reversed to reach a destination vertex D from a source vertex S, or if it is unreachable.

QUICK EXPLANATION:

The cost of an edge which exists can be considered as 0 and the cost of a reverse edge of an existing edge can be considered as 1. While we are taking the input of the edges of the graph, we insert two edges in the graph, the given edge with weight 0 and a reverse edge with weight 1. Then we can simply apply Dijkstra algorithm and find the minimum distance of the destination from the source or if it cannot be reached.

EXPLANATION:

A directed graph is given. We consider the weights of the given edges as 0. Now we can see that making a bidirectional edge means to reverse the given edges(which are not useful for reaching the destination). While we take input of the edges, for each edge we input two edges, the given edge and a reverse edge of the given edge. We consider the weight of the given edge as 0 and the weight of the reverse edge as 1(as that edge does not exists from before but we put it). Now by applying Dijkstra algorithm we can find the minimum distance of the destination vertex from the source vertex, i.e. the minimum number of edges we have reversed. If the destination vertex is unreachable from the source vertex, then we simply output -1.

The time complexity of this approach is proportional to the number of edges and vertices in the given graph. Using a priority queue for the Dijkstra algorithm, the time complexity will be O(n + m*log(m)). Also you need to use fast input-output otherwise you might get a TLE. You can find the implementation below.

AUTHOR'S AND EDITORIALIST'S SOLUTIONS:

Author's and editorialist’s solution can be found here.

ACTNBM – Editorial

$
0
0

PROBLEM LINK:

Practice
Contest

Author:Arkapravo Ghosh
Tester:Aparup Ghosh
Editorialist:Aparup Ghosh

DIFFICULTY:

CAKEWALK

PREREQUISITES:

Basic programming, conditionals

PROBLEM:

Given two integers S and H you need to output “Shinchan” if S > H or “Himawari” otherwise.

QUICK EXPLANATION:

According to the given conditions, Shinchan can only win when S is greater than H. In any other case, Himawari is the winner.

EXPLANATION:

We can simply check if S is greater than H or not. If S > H, then we output “Shinchan”(without quotes) else (i.e. when S <= H), we output “Himawari”(without quotes).
The time complexity is O(T).

AUTHOR'S AND EDITORIALIST'S SOLUTIONS:

Author's solution can be found here.
Editorialist’s solution can be found here.

RECIPE - Editorial

$
0
0

PROBLEM LINKS

Practice
Contest

DIFFICULTY

EASY

EXPLANATION

The problem here is divide all numbers by some constant so that the divisions have no remainder. We produce the smallest result by dividing by a number that is as large as possible, that is the greatest common divisor. The greatest common divisor can be computed efficiently by Euclid's algorithm, but in this case it was fast enough to simply check all numbers from 1 to 1000 for divisibility.

SETTER'S SOLUTION

Can be found here.

TESTER'S SOLUTION

Can be found here.


i m getting correct output on my laptop but showing wrong answer on codechef. TLG problem

$
0
0
n = int(input())
leadp1 = [0]
leadp2 = [0]
for round in range(n):
a,b = input().split(" ")
if (int(a)>int(b)):
    leadp1.append(int(a)-int(b))
elif (int(b)>int(a)):
    leadp2.append(int(b)-int(a))
if (max(leadp1)>max(leadp2)):
print("1",max(leadp1))
else:
print("2",max(leadp2))`

CAPIMOVE - Editorial

$
0
0

Practice
Contest

Author:Maksym Bevza
Tester:Arjun Arul
Editorialist:Misha Chorniy

Difficulty:

Simple

Pre-Requisites:

none

Problem Statement

You are given tree consisting $N$ nodes. Each node $i$ has value $P_{i}$ assigned to it. All values $P_{i}$ are distinct. For each node $j$ find maximal value of node in the graph if to remove nodes adjacent to $j$ and $j$.

Explanation

Subtask 1 and 2

$N$ is less or equal than 10000. For each node will mark all nodes which are adjacent to it as dead(not usable), will find maximum value between non-marked nodes.

for i= 1..N //Step 1 for j=1..N alive[j] = 1; //initialize all marks as ones //Step 2 alive[i] = 0; //mark itself as dead for v is adjacent to i //mark all adjacent nodes alive[v] = 0; //mark neighbours as dead ans[i] = 0; //Step 3 for j=1..N if alive[j]==1 //if node j is alive ans[i]= max(ans[i],p[j]); //find maximum value

How long it works? Obviously, the complexity of first and third step is $O(N)$, how to find the complexity of the second step? Denote $D(v)$ - degree of node $v$, for vertex $i$ complexity of step 2 is equal to $D(i)$, over all iterations complexity of second step is $D(1)$+$D(2)$+..+$D(N)$, what is $2*(N-1)$ for trees, we see that each edge using exactly two times. Total complexity of this algorithm is $O(N*N+2*(N-2)+N*N)$ = $O(N^2)$

First and third steps are slowest in the code above, how to optimize it? We can rewrite that code a bit. for i=1..N alive[i]=1; for i=1..N alive[i]=0; for v is adjacent to i alive[v] = 0; ans[i]=0; for j=1..N if alive[j] == 1 //if node j is non-marked ans[i] = max(ans[i],p[j]); //update answer alive[i]=1; for v is adjacent to i alive[v] = 1;

Now total complexity is $O(N+2*(N-2)+N*N+2*(N-2))$ = $O(N^2)$, still $O(N^2)$, we can make some observations, basically we have set, where the following operations can be performed:

  • $alive_{i}=1$ is equivalent to adding in the set value of $p_{i}$
  • $alive_{i}=0$ is equivalent to erasing in the set value of $p_{i}$
  • Subtask 3

    Let's use some data structure, namely in our case, the data structure which can add/erase elements, and find the maximal value in the set of these elements, but does this thing in time less than $O(N)$.

    for i=1..N add(P[i]); for i=1..N erase(P[i]); for v is adjacent to i erase(P[v]); ans[i] = getMaximalValue(); add(P[i]); for v is adjacent to i add(P[v]); What is the best data structure for these things, in most of the modern programming languages exists built-in data structures for such things, in C++ you can use set, Java has Set, Python has similar data structures. If your language doesn't have built-in data structures, you can read about heaps (http://pages.cs.wisc.edu/~vernon/cs367/notes/11.PRIORITY-Q.html) and realize it.

    Let's write code with the map in C++, the similar code can be written in Java with Map or in Python with the dictionary. set < int > S; for i=1..N // Step 0 S.insert(P[i]); //Initialize set //Adding element P[i] in the set for i=1..N //Step 1 S.erase(P[i]); //Erase value of node i from set for v is adjacent to i //Iterate over neighbours of node i S.erase(P[v]) //Erase values of neighbours of i //Step 2 if !S.empty() ans[i] = *S.rbegin(); //Value of greatest element //Step 3, rollback of the step 1 S.insert(P[i]); //Insert value of node i from set for v is adjacent to i //Iterate over neighbours of node i S.insert(P[v]) //Insert values of neighbours of i

    Complexity of adding/erasing of number in such data structures is $O(log N)$, summary complexity of the first steps is $O(N log N)$, the same is for third steps, Total complexity is $O(N log N + N log N + N log N + N log N)$ = $O(N log N)$

    The overall time complexity of this approach is $O(N log N)$.

    Solution:

    Setter's solution can be found here
    Tester's solution can be found here

    Please feel free to post comments if anything is not clear to you.

    TSECJ101 - Editorial

    $
    0
    0

    PROBLEM LINK:

    Contest

    Author:TSEC CodeCell

    DIFFICULTY:

    Easy-Medium

    PROBLEM:

    Find first index of array such that sum to the left side is equal to sum to the right side.

    QUICK EXPLANATION:

    Move from left to right, keeping track of sum to left and sum to right.

    EXPLANATION:

    First find total sum of array. Initialize left sum to 0 and right sum to total-a[0]. Move from left to right, adding a[i] to left sum and subtracting a[i] from right sum. Break whenever they become equal.

    AUTHOR'S SOLUTIONS:

    Author's solution can be found here.

    Where did I go wrong?

    TAJ from July LoC TLE

    Viewing all 40121 articles
    Browse latest View live


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