1 problem 2 solutions

This is in reference to the problem FACTORIAL (Problem code FCTRL)
link Factorial

One of solution got accepted:

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

 public class Main {
 public static void main(String [] args) throws IOException{
  BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
  String num = br.readLine();
  for (int i = 0; i < Integer.parseInt(num); i++){
   int counter = 0;
   int number = Integer.parseInt(br.readLine());
   int div = 5;
   while (div <= number){
    counter += number/div;
    div *= 5;
   }
   System.out.println(counter);

  }
 }
}

while the other is giving wrong answer:

    import java.util.*;
    import java.io.*;
    public class Main{
    
    public static void main(String[] args) throws IOException {
        InputStreamReader isr = new InputStreamReader(System.in);
        BufferedReader br = new BufferedReader(isr);
        int T = Integer.parseInt(br.readLine());
        int[] a = new int[T];
        for(int i=1; i<=T; i++){
            int N = Integer.parseInt(br.readLine());
            a[i-1]=N;}
        br.close();
        for(int i=0; i<T; i++){
            System.out.println(""+zeroes(a[i]));}}
    
    public static int highest(int p, int N){
        int result=p;
        int count=1;
        while(result*p<N){
            result*=p;
            count++;}
        return count;}
    
    public static int zeroes(int N){
        int power= highest(5,N);
        int result=0;
        int num=5;
        for(int i=1; i<=power; i++){
            result+=N/num;
            num*=5;}
        return result;}
}

But both solutions are basically the same, just different ways of writing.
Also both give the same result on many inputs I have tried on my compiler.
So why one one of them(first one) got accepted while the other(second one) is giving “wrong answer” according to codechef ?

you missed an equality in your function highest.

it should be

while(result*p<=N)

This should get you accepted.

3 Likes

thanks! got it