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

why my code is showing runtime error for MGCSET problem

$
0
0

include<iostream>

include<string>

using namespace std; int main() { int t; cin>>t; while(t--) { int n,m,flag=0; string ms,s; cin>>n>>m; for(int i=0;i<n;i++) cin="">>ms[i];

    for(int i=0;i<n;i++)
    {
        if(ms[i]%m==0)
        flag++;
    }
     for(int i=1;i<n-1;i++)
     {
         int sum=0;
         s=ms.substr(0,i);
         for(int j=0;j<s.length();j++)
         {
           sum+=s[j]-'o';  
         }
         if(sum%m==0&&sum!=0)
         flag++;
     }



    cout<<flag<<endl;
}



return 0;

}


CodeChef Assistant

$
0
0

CodeChef Assistant

CodeChef Assistant is an Android App which provides useful information to the CodeChef user. It includes 2 modules:

  • CodeChef Rating Predictor
  • CodeChef Attendance Master

CodeChef Rating Predictor

CodeChef Rating Predictor predicts the rating of the CodeChef user for the current contest if it is going on, otherwise it shows the rating predictions of the past contest, i.e., the previous contest.

So, the user can set his/her goal ( to achieve a certain rating ) for the contest and accordingly go for that as he knows the new rating beforehand during the contest.

CodeChef Attendance Master

CodeChef Attendance Master shows the attendance of the user and helps in determining the progress of the user towards 100% Attendance Participation Reward

100% Attendance Participation Reward

If a user participates (ie. solves at least 1 problem; and even partial solutions are counted) in each of CodeChef’s monthly contests (Long Challenge, CookOff, Lunchtime) in any 12 month period, then they will get 1000 Laddus. That is, if at any point, a user has participated in the last 36 CodeChef Monthly contests (ie. all the three contests in each of the last 12 months), then they will get 1000 Laddus.

So, the user can know how far he/she is to get the 100% attendance participation reward.

CodeChef Rating PredictorCodeChef Attendance Master
Link to the app: https://play.google.com/store/apps/details?id=tech.programmingbypc.codechefassistant

AreaPeri Error in java

$
0
0

import java.util.Scanner;

public class AreaPeri { public static void main(String args[]){ int length; int width; Scanner sc = new Scanner(System.in);

width=sc.nextInt();
length=sc.nextInt();
int area=width*length;
int perimeter=2*(width+length);

if(perimeter>area){
    System.out.print("Perimeter :" );
    System.out.println(perimeter);
        }
    else{

    System.out.print("Area");
    System.out.println(area);
            }          
 }

} /* I am uploading file or submitting this code showing below error this is my first question how to declare file name with extension java.

Main.java:3: error: class Collection is public, should be declared in a file named AreaPeri.java public class AreaPeri { ^ 1 error */

MANYCHEF - Editorial

$
0
0

PROBLEM LINKS

Practice
Contest

DIFFICULTY

SIMPLE

PREREQUISITES

Greedy

PROBLEM

We are given a string S. Each character of S is either a question mark ('?') or an uppercase letter ('A'-'Z'). Replace each question mark with an uppercase letter in such a way that the resulting string contains as many "CHEF" as possible. If there is more than one possible resulting string, output the lexicographically smallest one.

QUICK EXPLANATION

Iterate the string S from the end of the string to the beginning. Each time we can replace the current positions with the string "CHEF", we replace with it. Then, change all remaining question marks with character 'A's.

EXPLANATION

First, let's introduce some notations.

  • N = the number of characters of S.
  • S[i] = the i-th character of S (1-based).
  • S[i..j] = the substring of S consisting of the i-th character of S through the j-th character of S.

Then, let's introduce an intuitive yet important lemma.

Lemma 1. All occurrences of "CHEF"s in S must be non-overlapping.

Proof. The string "CHEF" cannot overlap with itself because it consists of 4 distinct characters. This is in contrast to, for example, string "ABAB", which can overlap with itself. In this case, we can have a string with two overlapping "ABAB"s: "ABABAB".

Now that Lemma 1 is proved, we can come up with the following theorem.

Theorem 1. Consider any string S. If we can replace S[1..4] with "CHEF", then it is optimal to replace it.

Proof. The proof is by contradiction. Suppose that S can contain at most X "CHEF"s. Since "CHEF"s cannot overlap each other, the next occurrences of "CHEF"s must be at least from position 5. If we do not replace S[1..4], all occurrences of "CHEF"s must be contained in S[5..N]. Let Y be the maximum number of "CHEF"s that S[5..N] can contain. For Theorem 1 to be not optimal, it must be the case that Y > X. But it is not impossible, because:
X = the maximum number of "CHEF"s in S[1..N]
= the maximum number of "CHEF"s in S[1..4] concatenated with S[5..N]
= 1 + Y.

By Theorem 1, we can derive the following greedy solution.

for i = 1; i ≤ N-3; i++:
    if can_replace(S[i..i+3], "CHEF"):
        replace(S[i..i+3], "CHEF")

Now, let's move to the next lemma.

Lemma 2. All remaining occurrences of '?'s in S that were not replaced by the previous iteration should be replaced with 'A'.

Proof. We want to have the lexicographically smallest resulting string. The lexicographically smallest letter is 'A', so it is always better to replace the question marks with 'A's.

It seems that now we have found a working solution. However, consider the case "???????". If we apply the previous solution, the resulting string would be "CHEFAAA", whereas the correct solution should be "AAACHEF" because it is lexicographically smaller. We can fix this easily by iterating the string S backwards (the previous lemma and theorem would still apply) so that all occurences of "CHEF"s are "as to the right as possible".

Here is the pseudocode of the final solution.

for i = N-3; i ≥ 1; i--:
    if can_replace(S[i-3.. i], "CHEF"):
        replace(S[i-3..i], "CHEF")
for i = N-3; i ≥ 1; i--:
    if S[i] == '?':
        S[i] = 'A'

The time complexity of this solution is O(N).

SETTER'S SOLUTION

Can be found here.

TESTER'S SOLUTION

Can be found here.

MockVita Round 2 2018

Innerve summer code challenge

MockVita Round 1 2018

i am unable to find the error in this code.please help me

$
0
0

include <iostream>

using namespace std; int main() { double x,y,o,mod; cin>>x; cin>>y;

mod=y;
while(mod>=x)
{mod = mod-x;
}
//  cout<<"mod is"<<mod;

if(mod==0.00)
{
    if(y>x)
    { //cout<<x<<" "<<y;

    o= ((y-x)-0.50);
    //cout<<o;
    }
    else
    {
    o=y;
    }

}
else
{
    o=y;
        }
        cout<<o;

return 0;

}


CHEFTMA - Editorial

$
0
0

PROBLEM LINK:

Practice
Contest

Author:Dmytro Berezin
Tester:Antoniuk Vasyl and Misha Chorniy
Editorialist:Pushkar Mishra

DIFFICULTY:

Simple

PREREQUISITES:

Greedy, In-built data structures

PROBLEM:

EXPLANATION:

Subtask 1
Given constraints are very small. Thus, a brute force solution can easily work. Trying all possible buttons in all possible ways and noting the minimum over all such arrangements gives the answer. The total complexity of this approach is $\mathcal{O}(2^N*2^M*2^K)$. This will pass under the given constraints.

Subtask 2
There are many observations to make here which reduce this problem substantially:

  • For the given $A[i]$ and $B[i]$ values, we are only interested in the value $A[i]-B[i]$. Basically, we can modify each $A[i]$ to $A[i]-B[i]$. From this point onwards in this editorial, array $A$ is assumed to be containing the modified values, i.e., $A[i]-B[i]$.

  • The black and white buttons fundamentally do the same things when dealing with the modified values of the array $A$. Thus, we put all the values from the two arrays, $C$ and $D$, in a common multiset called $buttons$. A multiset is a set which allows duplicates to exist.

Now, before we go on to the formal algorithm, let's think of a smaller problem. Let's take two values from the array $A$ (just reminding, the values aren't the ones which were taken as input) $A[i]$ and $A[j]$. Let's assume that $A[j] > A[i]$. Now, let us take two values $x$ and $y$ from the $buttons$ multiset. Let's say that $x$ < $y$ < $A[j]$. Which button should be use for $A[j]$ then. Intuition tells us that it is beneficial to use $y$. This is for two reasons. First one is that using $y$ allows us to complete more number of tasks on the $j^{th}$ day than $x$ using why would. Second reason is that assume $y$ > $A[i]$ and $x$ < $A[i]$. If we use $x$ on $A[j]$, we won't be able to use $y$ on $A[i]$. A better strategy is to use $x$ on $A[i]$ and $y$ on $A[j]$. What if both $x$ and $y$ were less than $A[i]$. Then we could use either of buttons on $A[i]$ and the other on $A[j]$. This is because we will anyway be reducing the same number of tasks from the sum of total incomplete tasks.

This gives us our greedy algorithm: for a particular value $A[i]$, use the button $x$ such that $x$ hasn't been used up till now and $x$ is the largest value less than or equal to $A[i]$ in the $buttons$ multiset. Now, the incomplete tasks left will be $A[i] - x$. Add this to the accumulator variable which is to be finally returned as the answer. $x$ is removed from the multiset since one button can't be used twice. The proof of this greedy algorithm has been given above and can be more formally stated as "Exchange Argument". You can find more about proofs of greedy algorithms here.

The multiset data structure can be found in almost all the major programming langauges. Writing one on your own requires knowledge of balanced binary search trees. All operations in a multiset are in order $\mathcal{O}(\log N)$.

The editorialist's program follows the editorial. Please see for implementation details.

OPTIMAL COMPLEXITY:

$\mathcal{O}(N\log N)$ per test case.

SAMPLE SOLUTIONS:

Author
Tester
Editorialist

New Whatsapp Group for discussing problems

Whatsapp group for competitive programming

New Whatsapp Group for discussing problems and helping others

$
0
0

Here is the new whatsapp group created by me:

https://chat.whatsapp.com/HtghtaONUzkJEIX2j0hEtI

You can post questions and discuss algorithms and data structure in this group. Everyone uses whatsapp and hence it should be more engaging than using other platforms.(from personal experience)

Also if we work as a group we keep getting motivated. I would request for good programmers to give it a try to help others in this group.

Does attempting only non scorable problems in a contest affect rating?

$
0
0

As a Div 1 participant, if in a Long Challenge, I ONLY attempt the problems from non-scorable section and do not attempt any problem from the scorable section, am I part of the contest, i.e. will my rating be affected?

Question regarding a practice problem

$
0
0

Hello everyone, I have a question regarding a practice problem named "Second largest". When I tried to submit my code, this page returns "Runtime error" but I have seen lengthier solutions to the problem that have more lines of code, but are marked as correct. I ran my code on my computer and it returns the correct output. I don't want to copy someone else's code, so I am asking for an advice.

Haven't received June long challenge laddus yet!!

$
0
0

Its almost been a month since i haven't received my laddus for the June long challenge 2018. No one seems to reply ,neither the admin nor anyone replies when i mail on winners@codechef.com.

Since i came 2nd in that long,they said that they'll give me once they check that i am the first time cash winner(There has been invented a new rule that cash winners will get laddus only for their first time). After having been confirmed about all those stuffs,i still haven't received it.

I dont know where to ask now,so i am sorry i had to ask over here.

@admin Please if possible give the laddus soon.


regarding wrong answer!! In my code Two Numbers in Beginners

$
0
0

can anyone explain that code why it got the wrong answer?TWONMS

Quadratic roots question C++14

$
0
0

The question : https://www..com/problems/QUADROOT

**#include <iostream>

include <math.h>

using namespace std; int main() { float a,b,c,x1,x2,d; cin>>a>>b>>c; d= (bb)-(4ac) x1= ((-b) + sqrt(d)) / (2a); x2= ((-b) - sqrt(d)) / (2a); cout<<x1<<endl<<x2; return 0; }*

copiler shows wrong output every single time !! Please help amazing coders!

Challenge problem doubt

$
0
0

Its mentioned that the score which we get is only for 1/4th of test files and rest files are hidden. But if we get non-AC on any of those hidden files the final verdict is also non-AC. Is it similar for TLE verdict? If i get TLE on any hidden file will it show TLE right now or later when contest gets over?

PALLIND - Editorial

$
0
0

PROBLEM LINK:

PracticeContest

Author:Vaibhav TulsyanTester:Aditya PaliwalEditorialist:Aswin Ashok

DIFFICULTY:

EASY

PREREQUISITES:

Expected Value

Modular Arithmetic

MMI

PROBLEM:

A short and concise description of the problem statement. 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.

EXPLANATION:

Given an empty string, Bob randomly picks a character from ‘a’ to ‘z’ and appends to the string and every time he appends he checks if the resultant string is a palindrome, if yes he get one point for that append operation. Bob does n such append operations and we are asked to find the expected value of the total number of points he gets. We have to find the answer $modulo 10^9 + 7$.

SOLUTION:

It is the sum of expected number of palindromes of length $i$, for i=1 to n. We can get the probability of getting a palindrome of length i by fixing the first i/2 characters and let the second i/2 characters be the mirror image of the first. For example: A string of length 4 will be palindromic if we fix its first two characters and let the next two be the mirror of the first two and for a String of length 5 we can fix the first three characters and let the last two characters be the mirror of first two. This works out to be $26^{ceil (i/2)}/26^i$ which is $f(i)= 1/26^{floor(i/2)}$. This is because $ceil(i/2) + floor(i/2) = i$ (for all integers)

The final answer is $\sum_{i=1}^{i=n} f(i)$

This becomes: $1 + 1/26 + 1/26 + 1/26^2 + 1/26^2 + …..+1/26^{floor(n/2}$

We can notice same terms repeating so we can re write the series

$1 + 2(1/26 + 1/26^2 + 1/26^3 + . . . + 1/26^{floor(n/2)})$ if n is odd

$1 + 2(1/26 + 1/26^2 + 1/26^3 + . . . + 1/26^{floor(n/2)}) + 1/26^{(n/2)}$ if n is even

Since n is very large we cannot evaluate every term. We can notice that $(1/26 + 1/26^2 + .. )$ is in GP so we can use sum of GP formula and Modular Multiplicative inverse to get the final answer.

TIME COMPLEXITY

To find sum of GP we have to find Modular Multiplicative inverse of the denominator in the sum of GP formula, which can be found out using Modular Exponentiation and the time taken for it is $O(logn)$ and last term if n is even can also be found out it $O(logn)$, Multiplication and addition can be performed in $O(1)$ so it can be done in $O(logn)$. Since there are t cases per file the overall complexity is $O(tlogn)$.

SOLUTIONS LINK:

Tester's solution can be found here.

Editorialist's solution can be found here.

Getting TLE Error...PLEASE HELP..!!!

$
0
0

I tried to reduce the size of code, but still i am getting the TLE error. Please look at my code and help me. Thanking You.link text

Viewing all 40121 articles
Browse latest View live


Latest Images

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