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

CHFJB – Editorial

$
0
0

PROBLEM LINK:

Practice
Contest

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

DIFFICULTY:

EASY

PREREQUISITES:

Primality test, factorization

PROBLEM:

Given a positive integer N, you need to find the number of factors of N except 1 and itself. If N is a prime number then simply output -1.

QUICK EXPLANATION:

We can find the number of factors of N except 1 and itself by iterating from 2 upto sqrt(N). If the no of factors in zero(0), then output -1(i.e. N is a prime) otherwise output the number of factors.

EXPLANATION:

According to the given problem we need to find the number of factors of N except 1 and itself, as it is mentioned that the job cannot be performed in 1 day(according to the instructions) or N days(as Chef will leave). So we iterate from 2 to sqrt(N) and keep a count of the number of factors of N. The idea of iterating from 2 to sqrt(N) is simple, you can identify it with a simple observation. For every number (< sqrt(N) ) which is a factor of N, there is another number(> sqrt(N) ) which is a factor of N too. So for every number(< sqrt(N) ), if it is a factor of N, we increase the count by 2. If the factor is equal to sqrt(N) (in case N is perfect square), then we increase the count by 1. After counting the number of factors of N, if the count is equal to 0(i.e. N is prime), we print -1, else we print the number of factors of N. You can find the solutions below.

AUTHOR'S AND EDITORIALIST'S SOLUTIONS:

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


Need solution which require math..

$
0
0

You are given a number N, we have to find the sum modulo 1e9+7 of a function which takes all number from 1 to N as input, function: given a number m calculate the x modulo m, where x is the product of number k iterating from 1 to m and (k, m) are coprimes, can I have a solution with math, code is not nescessary

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

ARRPROB – Editorial

$
0
0

PROBLEM LINK:

Practice
Contest

Author:Md Shahid
Editorialist:Arkapravo Ghosh

DIFFICULTY:

SIMPLE

PREREQUISITES:

Array

PROBLEM:

An array on N elements is given. We need to print an array of N elements where the element in the i^th index of the new array is the sum of all elements in the given array except the element at index i.

EXPLANATION:

We just need to find the sum of all elements in the array first. Then for each index i of the given array we need to print the (sum – i^th element) of the given array. The time complexity of this approach is O(n).

AUTHOR'S AND EDITORIALIST'S SOLUTIONS:

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

Code for SPELLBOB giving WRONG ANSWER in judge but working in IDE. What do I not understand?

$
0
0

My code for SPELLBOB is giving me a WRONG ANSWER (WA) in the judge but when I run it against the input given to us to test in the CodeChef IDE I get the right answers.

My understanding of the problem was that we are supposed to check whether the cards given to us have the ABILITY to spell 'bob' (2 b's and one 'o'), in any order 'bbo' 'obb' 'bob'. Is there something I am not understanding about the problem? Or is there something I am assuming that is wrong?

I don't need the answer to the problem, just an answer on whether my understanding of the problem is correct.

CodeChef beginner - The Lead Game (TLG) whats wrong in my code?

$
0
0
#include "bits/stdc++.h"
using namespace std;

int main()
{
    int t, s1, s2, lead;
    cin >> t;
    vector<int>p1;
    vector<int>p2;
    while(t--)
    {
        cin >> s1 >> s2;
        if(s1> s2)
            {
                p1.push_back(s1-s2);
            }
        else
            {
                p2.push_back(s2-s1);
            }
    }
    int max1 = *max_element(p1.begin(),p1.end());
    int max2 = *max_element(p2.begin(),p2.end());
    if( max1 > max2)
        cout << "1 " << max1;
    else
        cout << "2 " << max2;
}

where did I go wrong? please explain... submisssion for spell bob problem

TEST: Getting ce

$
0
0

Hi,

I used Eclipse to write the program Life, Universe, Everything. It compiles and works perfectly. However, when I submit, I get the message

Main.java:4: class LifeUniverseEverything is public, should be declared in a file named LifeUniverseEverything.java public class LifeUniverseEverything { ^ 1 error

My file is called LifeUniverseEverything.java

Here's my code, its more complex than necessary.


import java.io.*;
import java.util.*;

public class LifeUniverseEverything {

    //Private Variables
    private BufferedReader breader;

    //Constructor
    public LifeUniverseEverything() {
        breader = new BufferedReader(new InputStreamReader(System.in));
    }

    //Pre: User will only input numbers of one or two digits
    //Post: Returns an array consisting of all Strings the user inputs 
    public ArrayList<String> getInput() throws Exception{

         String s = null;
         ArrayList<String> input = new ArrayList<String>();
         try {
                 while((s=breader.readLine()) != null) {
                     input.add(s);
                 }

         } catch (Exception E) {
             System.out.println(E.getMessage());
             System.exit(1);
         }

         return input;

    }

    //Pre: result is an ArrayList of Strings. Result is not null
    //Post: Prints each string in result array. Stops when 42 is reached. 
    public void print(ArrayList<String> result) {
        for (int i=0; i<result.size(); i++) {
            if(result.get(i).equals(new String("42"))) {
                break;
            }
            System.out.println(result.get(i));
        }
    }

    public static void main(String[] args) throws Exception{
        LifeUniverseEverything start = new LifeUniverseEverything();
        ArrayList<String> result = start.getInput();
        start.print(result);

    }

}

getting "Wrong answer!"

$
0
0
import java.util.Scanner;

class ATM {

    public static void main(String args[]) {

        Scanner obj = new Scanner(System.in);
        System.out.println("Enter amount to be withdrawn");
        int wit = obj.nextInt();
        System.out.println("Enter initial balance");
        double bal = obj.nextDouble();
        if ((wit <= bal-0.5) && (wit % 5 == 0) && (wit > 0)) {

            System.out.println(String.format("%.2f", bal-wit-0.5));
        }
        else {

            System.out.println(String.format("%.2f", bal));
        }
    }
}

Why am I getting wrong answer here? What did I overlook?

This piece of code produces the desired results in my local computer just fine.

Getting a wrong answer in Bytelandian gold coins problem

$
0
0

test_case = 10 arr = [] for i in range(0,test_case): coin = int(input()) dollars = int(coin/2) + int(coin/3) + int(coin/4) if(dollars<coin): dollars = coin arr.append(dollars) for i in range(0,test_case): print(arr[i])

https://www.codechef.com/problems/COINS this is my code for the Bytelandian gold coins question with reference code COINS. Would you please solve my problem that on submission of this code to the problem i'm getting Wrong answer as feedback.

MISKEY-Editorial

$
0
0

PROBLEM LINK:

Contest
Practice

Author: Amit Kumar Pandey
Editorialist: Amit Kumar Pandey
Tester: Arkapravo Ghosh

Dificulty

Midium

PREREQUISITES

Ad-hoc, Dictionary

PROBLEMS

The issue with the keyboard is that the key placement is not appropriate. The keys of the right hand are faulty.

EXPLANATION:

The keys on the right hands are faulty. The keys are rotated by one place rightward. So, if one presses 'y' then 'p' is printed. So in the string printed by the keyboard, if 'p' is there then the user actually wanted to print 'y'. and so on.

AUTHOR'S AND EDITORIALIST'S SOLUTIONS:

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

I guess the logic and syntax are correct, but it shows the wrong answer, can someone guide please?

$
0
0

include<iostream>

using namespace std; int main(){ int n; cin>>n; int no[n][2]; int div[n]; for(int i=0; i<n; i++){="" for(int="" j="0;" j<2;="" j++){="" cin="">>no[i][j]; } } for(int i=0; i<n; i++){ for(int j=0; j<2; j++){ div[i]=(no[i][1])/(no[i][2]); } } for(int i=0; i<n; i++){ cout<<div[i]<<endl; } return 0; }

Do i need to write different code for different subtasks of a problem??....and what does subtask original constraints means??

$
0
0

I solved a problem from august long challenge.....my answer was partially accepted....it showed that subtask #2 have wrong answer...so i want to ask do i need to write different code for different subtasks....and what does subtask original constraints means?? Thank YOu

shknum august challenge

$
0
0

how can use power function pow in the codechef

PROBLEMS in aug challenge div2

$
0
0

i have written code for this prob but it is not accepting. i dont know whats the mistake i am getting correct ans for all my custom test cases. please someone provide some tricky test case with ans.


C++ Exception Handling

$
0
0

Can someone help me out in C++ ?

suppose i want to check whether an input number ( by user ) is even or not.. then i ask user to input a number ( only digit numbers )..and then i can simply check.

But Now , I want to handle all non digit input..so if user inputs any non digit value , then i want to throw an exception and want to handle that exception properly with the message "Enter the number again : " and repeat this process untill user inputs a digit value ( so that i could check even or odd ).

Please help me with the proper solution..I have spent my 2+ hours in this , but i m not able to get the correct solution.

*Any help would be appreciated..

getting a runtime error

$
0
0

include<stdio.h>

include<string.h>

int main() { int T,i; scanf("%d",&T); for(i=0;i<T;i++) { int K,N,j; //array of strings scanf("%d%d",&N,&K); char A[N][100]; char B[10][100][100]; for(j=0;j<N;j++) { scanf("%s",A[j]); // N strings denoting a dictionary of the forgotten language. } int L,k; for(j=0;j<K;j++) {

    scanf("%d",&L);

    for(k=0;k<L;k++)
    {
    //input of the fraces
    scanf("%s",B[j][k]);

    }
}

int flag,l;
for(j=0;j<N;j++)
{

    for(k=0;k<K;k++)
    {   flag=0;
        for(l=0;l<L;l++)
        {
            if(strcmp(A[j],B[k][l])==0)
          {
            flag=1;
            break;
            }
        }

    }
    if(flag==1)
    {
        printf("YES\n");
    }
    else
    {
        printf("NO\n");
    }
}

} }

The hardest gcd problem-??

$
0
0

I recently attempted the question the hardest gcd problem. I made a programme which first calculates the gcd of the entire array. then it factorises this gcd and checks whether all its factors are less than or equal to the given number key. if any factor or (the gcd when it happens to be prime) is greater than the number K, it prints 'NO'. The following is he code for it. Pl help to tell me where the errors are:-

#include <iostream>
#include<cmath>

using namespace std;



int g(int a, int b)
{
    if(a==0)
    {
        return b;
    }
    return g(b%a,a);
}

int gcdall(int * arr,int n)
{
    int result;
    result=arr[0];
    for(int i=1;i<n;i++)
    {
        result=g(result,arr[i]);
    }
    return result;
}

void print(int key, int k)
{
    int flag=0,prime=1;
    for(int i=2;i<=(key/2);i++)
    {
        if(key%i==0)
        {
            prime=0;
            if(i>k)
            {
                flag=1;
                break;
            }
        }
    }
    if(prime==1 && key>k)
    {
        flag=1;
    }
    if(flag==1)
    {
        cout<<"NO\n";
    }
    else
    {
        cout<<"YES\n";
    }
}

int main()
{
    int t;
    cin>>t;
    for(int i=0;i<t;i++)
    {
        int n,k;
        cin>>n>>k;
        int * arr;
        arr=new int[n];
        for(int i=0;i<n;i++)
        {
            cin>>arr[i];
        }
        int key=gcdall(arr,n);
        print(key,k);
    }
}

largest good subarray - score 15% - what is the basis of scoring?

$
0
0

My code solves many test cases. But score is only 15%. May i know the method of scoring and code suggestion

Here is my code:

/************

Welcome to GDB Online. GDB online is an online compiler and debugger tool for C, C++, Python, PHP, Ruby, C#, VB, Perl, Swift, Prolog, Javascript, Pascal, HTML, CSS, JS Code, Compile, Run and Debug online from anywhere in world.

*************/

include <iostream>

int main() { int T, cols, k, i; //std::cout << "Number of test case " << std::endl; std::cin >> T; if(T>10) { return 0; } int good[T];

for(k=0;k<T;k++) {

    //std::cout << "Enter size of array " << std::endl;
    std::cin >> cols;
    if(cols > 100000) {
        return 0;
    }
    int i,j,m=0;
    int* matrix = new int[cols];

    //std::cout << "Enter Elements : " << std::endl;
    for(j=0;j<cols;j++) {
        std::cin >> matrix[j];
        if(matrix[j] > 1000000000) {
            return 0;
        }
    }

    /*for(j=0; j<cols;j++) {
        std::cout << "Element : " << j  << " : " << matrix[j] << "\n";
    }*/

    for(i=0; i<cols; i++) {
        for(j=i+1; j<cols;j++) {
            if(matrix[i] > matrix[j]) {
                if(j-i+1 > m) m=j-i+1;
            }
        }
    }

    good[k] = m;
    //std::cout << "Highest is : " << m << "\n";
}

for(i=0; i<T; i++) {
    std::cout << good[i] << "\n";
}
return 0;

}

rainbow problom(i am getting a wrong answer)

$
0
0

include<stdio.h>

include<string.h>

int main() { int T,i; scanf("%d",&T); for(i=0;i<T;i++) { int K,N,j,flag=0; scanf("%d",&N); int A[N]; for(j=0;j<N;j++) { scanf("%d",&A[j]); } for(j=0;j<N/2;j++) { if(A[j]!=A[N-j-1]) {

        flag=1;
        break;

    }
}
int count=1;
if(N%2==0)
{
for(j=0;j<N/2;j++)
{
    if(A[j]!=count)
    {
        count++;
        if(A[j]!=count)
        {
            flag==1;
        }
    }
}

} else { for(j=0;j<=(N/2);j++) { if(A[j]!=count) { count++; if(A[j]!=count) { flag=1; } } }

} if(flag==1) { printf("NO\n"); } if(flag==0) { printf("YES\n"); } } }

Viewing all 40121 articles
Browse latest View live


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