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

Group For C++/C/Java/Python Students

$
0
0

Update: 8/11/16 created a group on edmodo, join via this link: https://www.edmodo.com/home#/join/2s9zic

Hello, friends I am a student. And a beginner in c++ programming. Friends i got an idea of creating a group for all C++/C/Java/Python students, the main idea is to solve and discuss problems of easy and moderate level, actually of CBSE and above standard, which will help us to compete in different competitions, scoring in academics and also to develop our skills in the language. The group will be either on Facebook or Edmodo. If interested you can add your Email in the answers. Thank You!


my code is not running

$
0
0

include<conio.h>

include<stdio.h>

main() { char x,A,E,I,O,U; printf("ENTER THE ALPHABET YOU WANT TO CHECK"); scanf("%d",&x); if(x== A||E||I||O||U) { printf("vowel"); } else { printf("CONSONANT"); } }

Python input error

$
0
0

When i try to run a code in python on codechef it gives EOF error.

But the same thing if run with custom input it runs perfectly.

https://www.codechef.com/problems/HS08TEST

$
0
0

Whenever run this code i get wrong answer can someone help me in notifying the error: code:

include<iostream>

include<iomanip>

using namespace std; int main() { int x,i; float bal; cout<<"\nEnter the balance amount"; cin>>bal; cout<<"\nEnter withdraw amount"; cin>>x; if(x%5==0 && x+0.5<bal) { bal=bal-x-0.50; cout<<setprecision(2)<<bal; } else cout<<setprecision(2)<<bal; return 0; }

T23 - Editorial

$
0
0

PROBLEM LINK:

[Contest] T23

Author:Nandishwar Garg

DIFFICULTY:

SIMPLE

PROBLEM:

You are given an array of size N. You have to find every possible subset of the elements in array. Then we have to check 3 conditions on these subsets:

  1. It should not be empty.
  2. It should not have any element in subset which divide other elements present in subset except 1.
  3. It should not have duplicate elements.

If it satisfies all the conditions then print yes otherwise print no.

EXPLANATION:

Suppose the array elements are 1,2 and 3. The subsets of these elements are {1}, {2}, {3}, {1,2}, {1,3}, {2,3}, {1,2,3}.

  1. None of these subsets are empty
  2. No element other than 1 is dividing another element
  3. No duplicate element is present.

So, you have to print yes.

Taking another example, the array elements are 2 and 4. The subsets of following elements are {2}, {4} and {2,4}.

  1. None of these subsets are empty
  2. In {2,4}, 2 is dividing 4 so it doesn’t satisfy the condition

Thus you have to print no as the output.

AUTHOR'S SOLUTIONS:

Author's solution can be found here

EOF error but works perfectly with custom input

$
0
0
def nCm(n,m):
if m==0:
    return 1
if m==n:
    return 1

return int((n/m)*nCm(n-1,m-1))

num_test=int(input())

for test in range(num_test):

N,K=[s for s in map(int, input().split())]

print(nCm(N-1,K-1))'

Why does it give EOF error?

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;

        } 

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


SMPAIR - Editorial

$
0
0

Problem link : contestpractice

Difficulty : CakeWalk

Pre-requisites : Sorting

Problem : Given a sequence a1, a2, ..., aN. Find the smallest possible value of ai + aj, where 1 ≤ i< jN

Explanation

This problem was the easiest one in the set and it was intended to enable everybody to get some points.

How to get 13 points

Here you have only two integers a1 and a2, so the only possible sum will be a1+a2.

How to get 60 points

The constraints were designed in such a way that you can iterate through all the possible pairs (i, j), where 1 ≤ i< jN and check for every obtained sum, whether it's the minimal one.

How to get 100 points

The answer is basically the sum of the minimal and the second-minimal element in the array. So you can simply iterate through all the numbers, keeping track of the minimal and the second-minimal number. Or if your programming language has built-in sorting, you can act even simpler, because after the sorting of the array in increasing order, the required minimal and the second-minimal will be the first and the second elements of the sorted array.

Related links

  • Reference in the built-in sorting for C++ users

Solutions : settertester

T21 - Editorial

$
0
0

PROBLEM LINK:

[Contest] T21

Author:Nandishwar Garg

DIFFICULTY:

SIMPLE

PREREQUISITES:

None

PROBLEM:

You are given two large numbers m and n. You have to multiply m and n then product is needed to be divided by 3. What will be the remainder after division?

EXPLANATION:

To solve the problem, just go through these 3 steps:

Step 1: The given numbers m and n are needed to be multiplied.

Step 2: The product which you get after multiplying the numbers is needed to be divided by 3.

Step 3: Print the remainder you get.

AUTHOR'S SOLUTION:

Author's solution can be found here

how can i call inner class method ......in prog.

$
0
0

abstract class AbstractDemo

{

public abstract void shankar();

class Inner
{
    static void show(){
    System.out.println("inner class E");}
}

}

class AbstractDemoTest extends AbstractDemo

{

public void shankar()
{
    System.out.println("mera name jai shankar he");
}
public static void main(String...s)
{
    AbstractDemoTest oo= new AbstractDemoTest();
    oo.shankar();
    Inner.show();
}

}

spell bob august challenge..whats wrong in my code....the error being shown is run time eror

$
0
0
#include<iostream>

using namespace std; int check(char exp1[],char expp2[]); int main() { int t; cin>>t;

int k=0;
    int a[k]={0};

while(t-->0)
{

    char exp1[3],expp2[3];
    cin>>exp1;
    cin>>expp2;
    int countb=0,counto=0,i,j;
    int f=check(exp1,expp2);
    for(i=0;i<3;i++)
    {
        if(exp1[i]=='b')
        countb++;
        else if(exp1[i]=='o')
        counto++;
        else
        continue;
    }

    for(i=0;i<3;i++)
    {
        if(expp2[i]=='b')
        countb++;
        else if(expp2[i]=='o')
        counto++;
        else
        continue;
    }

    if(f==1&&countb>=2&&counto>=1)
    a[k++]=1;
    else
    a[k++]=0;
//  cout<<"t="<<t<<endl;

}
for(int i=0;i<k;i++)
{
    if(a[i]==1)
    {
        cout<<"yes"<<endl;
    }
    else
    {
        cout<<"no"<<endl;
    }
}

} int check(char exp1[],char expp2[]) { int i,j; for(i=0;i<3;i++) { if((exp1[i]=='b'||exp1[i]=='o')||(expp2[i]=='b'||expp2[i]=='o')) continue; else break; } //cout<<"i="<<i<<endl; if(i==3) return 1; else return 0; }

what kind of questions may i ask?

$
0
0

I joined today in this site.I am poor at coding.By the suggestion of my sir, I want to know this site and want to learn coding.I am a student.I am studying Btech 1st year in iiit nuzvid.Please give some better suggestions/tricks for me.

Help on Fast input/output

$
0
0

can someone explain me what's fast input/output. I m sorta new to this stuff and would really appreciate some help. Thank you.

Someone reply For these test case

$
0
0

In order to test my understanding of problem statement.

problem link:- link text

     6 3

    16 24 60
    498 861 589

    14 24 62
    72 557 819

    16 15 69
    435 779 232

    1 9 19
    1 2 3

    9 99 999
    9 9 9

    7 8 9
    3 2 5

Output :-

5 4 2 6 1 3

Is this correct or im missing something??


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.

GRDPRTY - Editorial

$
0
0

PROBLEM LINK:
Practice
Contest

Author:Arkapravo Ghosh
Editorialist:Arkapravo Ghosh

DIFFICULTY:

EASY

PREREQUISITES:

Math Triangular Number

PROBLEM:

The problem asks to find out the no. of elements in one side of the triangle formed from the total no. of unit elements. Simultaneously, the total user input number needs to check if it is a triangle number or not.

EXPLANATION:

The square root of n, one can define the (positive) triangular root of n as the number x such that Tx = n.
x can be calculated by this: x = (sqrt(8*n+1)-1)/2
which follows immediately from the quadratic formula. So an integer n is triangular if and only if 8n + 1 is a square. Equivalently, if the positive triangular root x of n is an integer, then n is the xth triangular number.

AUTHOR'S AND TESTER'S SOLUTIONS:

Author's solution can be found here

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.

Invitation to Encoding August '18

$
0
0

Hello everyone!
We are excited to invite you all to Encoding August '18. The contest is hosted by Netaji Subhash Engineering College, Kolkata. The contest will have challenges for all - beginner to advanced programmers. We hope all of you will love to join us and spend the Sunday evening solving exciting coding challenges.

The authors of the contest are:
1. Arkapravo Ghosh
2. Amit Kumar Pandey
3. Rahul Ojha
4. Md Shahid

Contest Details

Date : 5th August
Time : 19:30 hrs - 22:30 hrs(IST)Check your timezone here
Contest Link : https://www.codechef.com/ENAU2018
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.

Good Luck!
See you on the ranklist! Happy Coding!

Viewing all 40121 articles
Browse latest View live