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

Chef the Brave Warrior Editorial

$
0
0

Problem Link-https://www.codechef.com/problems/EXOCODE5

Author:https://www.codechef.com/users/vivek96

DIFFICULTY:Easy-Medium

PREREQUISITES-Big-Integer,Basic Java Programming

PROBLEM:Chef is a brave Warrior who with his group of young soldiers moves from one place to another to fight against his opponents. Before Fighting he just calculates two things,the difference between his soldier number and the type of opponent’s soldier number.

From this difference and the opponent army soldier number type (even or odd number)?, he decides whether to fight or not?

Print the difference and the type of opponent army soldier number!

Chef ‘s opponent number is never greater than Chef number.

Constraints:The numbers are less than 10^19

EXPLANATION:From the Question statement it is crystal clear that we have to find difference between chef army soldier number and opponent soldier number. Numbers are large so we have to use Big-Integer Class(The java.math.BigInteger class provides operations analogues to all of Java's primitive integer operators and for all relevant methods from java.lang.Math.)

Introduction to Big Integer-https://goo.gl/WZis9f

IN C/C++ -http://codeforces.com/blog/entry/16380

Using BigInteger class function subtract we calculated the difference,then we have to check whether opponent soldier number is even or odd. (A number is said to be even number if it leaves no remainder when divided by 2. There is an alternative definition of even number and it is as a number having any number from 0, 2, 4, 6 and 8 at its ones place is an even number. Examples of even numbers are 12, 66, 456, 9900 and 12342 etc. An odd number leaves a remainder when it is divided by 2. All those numbers having any one from 1, 3, 5, 7 and 9 at their ones places are also called odd numbers.)

So for Checking Whether number is even or odd we can use remainder function,so if number is even we have to print even else print odd

AUTHOR'S AND TESTER'S SOLUTIONS:

import java.util.Scanner;

import java.math.BigInteger;

class ChefBraveWarrior {

public static void main(String []arg)

{
    Scanner in = new Scanner(System.in);

    BigInteger a = new BigInteger(in.next());

    BigInteger b = new BigInteger(in.next());

    BigInteger diff = a.subtract(b);

    if(b.remainder(BigInteger.valueOf(2))==BigInteger.ZERO)
      {
        System.out.println(diff);
        System.out.println("even");
      }
    else
        {
        System.out.println(diff);
        System.out.println("odd");

    }
}

}


DataBread : Tool to Track your Algorithmic Ratings.

$
0
0

Hello Coders

Hope you are having a great time. I take the pleasure in introducing you to DataBread where you can see all your friends ratings from different competitive programming websites (Codechef, SPOJ, HackerEarth, HackerRank and Codeforces for now) at one place (called databread ratings).

It includes a leaderboard which you can filter by your Institution or Country and compare your ratings with your friends easily. Find your friends easily and view their profile, all at a single place.

It also includes a feature to link your linkedIn profile on your profile page. Feel free to contribute.

I would love to hear if you have some great suggestions or found any bugs

EDIT: So guys Finally the site is completely up, Hackerrank Ratings also working now, Leaderboard will be updated in a few minutes. You may wish to check your standings now.

And here's our new facebook page. If you like databread then share.

EDIT:

Added option to sharedatabread with your friends.

EDIT

Small but an important update. Find out here.

So for now

All The Best

Code Hard

Editorial for KBIGNUMB required!!

Please share , Your Logic for solving INTERVAL feb17

$
0
0

What was the logic in this question . I could solve it only for m=2 . Please explain the logic in simple way , I am a mere beginner.

Please Correct my logic for SCHEDULE March 17

$
0
0

I calculated all the continuous lengths(1,2,6,7....) , then I went on reducing k(k--) to convert all segments(l_seg) of a particular length (z) into three segments of lengths 1,(2 of l_seg/2)(odd), (l_seg/2,l_seg/2 -1)(even) and removed the original length and it's count by these three lengths of samecount . When the highest length reached 2 I calculated the minimum no of flips required to make it 1 by adding the alternate distances between segments of length 2 and then taking the minimum of two len, if it was less than k , ans is 1 or its two . at any point if k becomes 0 ans is the length of next big segment , if k becomes less than 0 than ans is that particular segment.

Please tell me where is it wrong , I was getting 5 of 9 cases correct.

Need Karma Points

$
0
0

Please upvote me. Need karma points to ask questions. Thanks !

Invitation to Alkhwarizm-2017

$
0
0

alt text
The much awaited signature event of IIIT Allahabad's Tech Fest Aparoksha 2017- ALKHWARIZM is here to endeavour with a challenging problem set.
It will be a 5 hour individual contest with algorithmic problem-set of diverse nature to be hosted on Codechef.
Contest Link is here - ALKHWARIZM-2017
Prize money worth INR 30k and other exciting goodies await you.
CODE ON!!
So be ready to have a nail biting experience on March 24th, 2017 - 9 pm to 2 am IST.
Register right now at the link given below to be eligible for prize money -
Form

Problem Setters- Man Mohan Mishra , Shiv Dhingra , Shivam Garg , Ankit Rao

Facebook Event page - Event

CHEFYODA unofficial Editorial

$
0
0

CHEFYODA
Contest link
Let's consider Game1 in which only horizontal and vertical movements are allowed, One can easily see that yoda wins only when both N and M are odd.

In the other game, chef wins only when both N and M are even.

This reduces the problem to following

  • If chef is winning in both the games, output 1
  • If chef is losing in both the games, and P $\neq$ 0 , output 0. Else if P = 0, then output 1.
  • If chef is losing in 1 game and winning in other, then we need to calculate probbability of winning atleast P games out of K.

    This is given by binomial distribution as $\frac1 2^K $ $\sum_{i=P}^{K}$ $K\choose i$

    Now the task is to calculate $K\choose i$ for i = P to K. You can calculate this by using BigDecimal in Java or using Decimal in python. This would work for small test case and would give you 40 points. For large testcase, this would result in TLE, even if you set precision to 6 decimal places.

    Now, sum of last K - P terms is equal to sum of first K - P terms

    $\sum_{i=P}^{K}$ $K\choose i$ = $\sum_{i=0}^{K - P}$ $K\choose i$

    and as per following

    $\sum_{i=0}^{K - P}$ $ \le 2^{K - 1} \exp\frac{(K - 2(K - P) - 2)^2}{4(1+(K - P) -K)}$

    since Probability is $\frac1 2^K $ $\sum_{i=0}^{K - P}$ $K\choose i$, we have

    Probability $\le 2 ^{-1} \exp\frac{(K - 2(K - P) - 2)^2}{4(1+(K - P) -K)}$

    Now, for small values of P, probability would be close to 1 and we could answer 1 instead of finding out probability. Similarly, for large values of P, probability would be close to 0 and we could answer 0 instead of finding the probability.

    The first difference in output would occur when Probability = $10^{-6}$

    We can find the value of P by following

    $10 ^ {-6} = 2 ^{-1} \exp\frac{(K - 2(K - P) - 2)^2}{4(1+(K - P) -K)}$

    Substituting K = $10 ^ 5$ gives P = 49162 and 50821 Substituting K = $10 ^ 6$ gives P = 497370 and 502614

    Observe that these values are pretty close to $ \frac K 2$. We know that sum of all terms is $2^K$ and that the series is symetric. Therefore, sum of $ \frac K 2$ terms is $2^{K - 1}$. Now, we need to add or subtract atmost 3000 Terms from $2^{K - 1}$ to get probability.

    This can be done within the given timelimit and here is thecode

    You can also do it using Python Libraries code


SPOJ - HISTOGRA - Largest Rectangle in a Histogram help needed

$
0
0

Since I couldn't solve the problem myself, I read up the concept and codes and managed to get it submitted via a Divide and Conquer (divide from middle) approach and a stack based O(n) solution. I also came up with a segment tree solution but it seems to be giving me a wrong answer. I've referred to different segment tree solutions but can't seem to fix mine. Can anyone check out my approach and tell me what's wrong here? (It gives me correct answer on the test cases provided)

http://ideone.com/VDtOML

Thanks!

What is offline query?

$
0
0

While trying to solve http://www.spoj.com/problems/DQUERY/en/, I came across an offline solution. It sorted the queries before hand and solved it. Can anyone help me understand the approach? How can storing the queries beforehand solve the problem?

CODEZILLA Hidden Solutions

$
0
0

Why the solutions of CODEZILLA contest are not visible to us @admin? Is it a private contest or there is any BUG in that page?

Kickstart Round A

$
0
0

Hello everyone, There will be a youtube livestream of problem discussion of Round A (Kickstart 2017) by Akashdeep Nain. Also in store is some discussion regarding DS, Algorithms and some tips on how to solve these kind of problems. The link is - https://www.youtube.com/watch?v=WvXbJ8XuGIw It starts at 9:00 PM IST today.

Good luck and Happy learning

HS08TEST - Editorial

$
0
0

PROBLEM LINK:

Practice

Author:ADMIN

Editorialist:SUSHANT AGARWAL

DIFFICULTY:

CAKEWALK

PREREQUISITES:

Basic looping,Basic Input/Output,Data Types

EXPLANATION:

Please refer to the sample solution given by editorialist.

EDITORIALIST'S SOLUTION:

Editorialist's solution can be found here.

AC solution in Python for Chef the Brave Warrior.

Ratings for Institutes In Competetive Programming (CpCtrl.com)

$
0
0

Hello Everyone,

Codechef has been a great platform for motivating and guiding potential competitive programmers on the individual basis. Ratings for all the contests: Long, Short and Lunchtime keep the sense of competition ongoing.

To provide and promote competition on the institution level, I designed a rating list provides unofficial ratings for all institutes ln the basis of Long contests of Codechef. Check it here.

The rating system works on the basis of ELO rating system. For more information about the entire rating system, please check this.

This rating system is entirely unofficial but does motivate the performance on institute level.

Future Additions to the Website:

  • Rating System for Short and Lunchtime challenges.
  • Full Event description regarding total participants and submissions grouped into colleges.

Any more inputs would be most welcomed from all programmers to improve this platform.I hope you all like it.

Edit- Now you can check ratings for cook-off events also.


#include - Compile Time

$
0
0

Is it good to use bits/stdc++.h header file in Codechef or any other competitive platforms? Because according to this link, the first answer says it increases compilation time. In general, I want to know some advantages of using this header, though I know it includes all precompiled headers.

MAKETRI: Getting WA only for 2 cases. Rest AC. What's wrong with my code?

$
0
0

I get WA on Tasks 15 and 16 and AC on the rest. I am not able to spot the bug. Could someone help. Thanks!

Headers included is bits stdc++.h

using namespace std;

long long min(long long a, long long b) {
    if ( a <= b ) {
        return a;
    }
    else {
        return b;
    }
}


long long max(long long a, long long b) {
    if (a >= b ) {
        return a;
    }
    else {
        return b;
    }
}


int main(void) {

    long long n, l, r;

    cin>>n>>l>>r;


    vector<long long> a, left, right;

    long long tmp;
    for (int i = 0; i < n; i++ ) {
        scanf("%lld", &tmp);
        a.push_back(tmp);
    }

    sort(a.begin(), a.end());

    long long le, ri;
    for (int i = 0; i < (n - 1); i++ ) {
        le = a[i+1] - a[i] + 1;
        ri = a[i] + a[i+1] - 1;
        left.push_back(le);
        right.push_back(ri);
    }

    long long res = 0;
    long long endIndex, startValue, currentIndex;
    currentIndex = endIndex = n - 2;
    bool shouldBreakOut = false;


    while ( currentIndex > 0 ) {

        if (right[endIndex] < l) {
            shouldBreakOut = true;
            break;
        }

        startValue = left[endIndex];

        while( ( currentIndex > 0 ) && (startValue <= right[currentIndex-1]) ) {
            currentIndex--;
            if (startValue > left[currentIndex]) {
                startValue = left[currentIndex];
            }
        }


        if (currentIndex > 0) {
            long long rightVal, leftVal;

            rightVal = min(r,right[endIndex]);

            if ( startValue <= l ) {
                shouldBreakOut = true;
                leftVal = l;
            }
            else {
                leftVal = startValue;
            }

            res += (rightVal - leftVal + 1);

            if (shouldBreakOut == true ) {
                break;
            }
            else {
                endIndex = currentIndex - 1;
                currentIndex = endIndex;
            }
        }
    }


    if (n > 2 ) {
        if ( shouldBreakOut == false ) {

            long long leftVal = min(startValue, left[currentIndex]);
            long long rightVal = right[endIndex];

            if ( leftVal <= r && rightVal >= l) {
                leftVal = max(leftVal, l);
                rightVal = min(r, rightVal);
                res += (rightVal - leftVal + 1);
            }
        }
    }
    else {
        long long leftVal = left[0];
        long long rightVal = right[0];

        if ( leftVal <= r && rightVal >= l ) {
            leftVal = max(leftVal, l);
            rightVal = min(rightVal, r);
            res += (rightVal - leftVal + 1);
        }
    }

    printf("%lld\n", res);

    return 0;
}

SSQ - Editorial

$
0
0

PROBLEM LINK:

Practice
Contest

DIFFICULTY:

EASY-MEDIUM

PREREQUISITES:

DFS, Knapsack

PROBLEM:

We are given a tree, and a set of cost and items associated with each node. We have to compute the optimal spending of 'k' rupees to buy maximum number of items, is we start at the root. and travel each edge exactly once.

QUICK EXPLANATION:

Get all the root to leaf paths using DFS.
On reaching a leaf, use knapsack to calculate the max no. of items in that root to leaf path.

EXPLANATION:

On seeing the question, we notice that it is a tree. So there won't be multiple cycles. Imagine the same question asked for tree whose nodes are connected in a straight line like $1<->2<->3<->.........<->(n-1)<->n$
The problem breaks down to a knapsack. So we convert the given problem into the desired form We can get all the unique root to leaf paths by a single DFS traversal. On reaching a leaf node, we use the knapsack to calculate the maximum acheivable value in that path.

dfs(node, stack, visited):
    visited[node] = true
    stack.push(node)
    leaf = true
    for neighbours of node:
        if visited[neighbour] = false:
            dfs(neighbout, stack, visited)
            leaf = false
    if leaf == true:
        Knapsack(stack)
    stack.pop()

ALTERNATIVE SOLUTION:

Maintain a knapsack at each node and add to it while travelling to children.

GMQ - Editorial

$
0
0

PROBLEM LINK:

Practice
Contest

DIFFICULTY:

EASY

PREREQUISITES:

Factorization

PROBLEM:

Given an array, we have to give the sum of factors from L to R

QUICK EXPLANATION:

Store the factorized sum of each element while taking input.
Store the cumulative sum and return $a[r]-a[l-1]$ for each query.

EXPLANATION:

This is a straight away question.
You just need to factorize an element and store all the factors.

for i in (1..n)
    fact[i] = fact[i-1] + factorize(a[i])
for each query
        print fact[r]-fact[l-1]

KSQ - Editorial

$
0
0

PROBLEM LINK:

Practice
Contest

DIFFICULTY:

EASY

PREREQUISITES:

Strings

PROBLEM:

We have to check if there exists a string in the given set of strings which can be written by using only the first terms of the set of strings.

QUICK EXPLANATION:

Store all the first letters of the string in an array, and then check each string.

EXPLANATION:

We are given that n,|s| < 100 so we can iterate each string.
We store the first characters of each string while taking the input.
Then we need iterate through all the strings to check if the given string is possible to be written using those first letters.

for i in (1..n):
    cin >> a[i]
    firstLetters[a[i][0]]++
kind=false
for i in (1..n):
    currKind=true
    for each character in a[i]:
        curr[character]++
    for c in ('a'..'z'):
        if(curr[c]>firstLetters[c]):
            currKind=false
    if currKind == true:
        kind = true

Time Complexity: O(n*|s|)

Viewing all 39796 articles
Browse latest View live


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