this is in regards to the problem Sherlock And Counting. https://www.hackerrank.com/challenges/sherlock-and-counting
I've used diffchecker to compare thousands of results that my code has produced vs the results that I've bought using my hackos. They are the same! My code is returning faulty in spite of my results being the same as the results bought using the hackos.
I've removed the diffchecker link because it's too large and freezes the tab but I've tested the first 2000 results and my results are the same as the results bought using the hackos. Simply put, I cannot understand why Hackerrank does not accept my code? You can try inputting this code and noticing that you do indeed get the same results as the test cases (bought using the hackos) but somehow it doesn't accept it?
import java.io.*;
import java.util.*;
import java.math.*;
public class Solution {
public static void main(String[] args) {
/* Enter your code here. Read input from STDIN. Print output to STDOUT.
Your class should be named Solution. */
Scanner in = new Scanner(System.in);
int testcases = in.nextInt();
/* Each individual test case */
for(int index = 0; index < testcases; ++index)
{
long N = in.nextLong();
long K = in.nextLong();
/* Notice that we require i < N and i to be a positive integer.
This is impossible if N == 1. Therefore, a simple test solves
a big issue. */
if(N <= 1){
System.out.println(0);
}
/* Test if discriminant is negative. If it is, the number of
integers is N - 1 because every number fits under the graph. */
else if(N*N - 4*N*K < 0)
{
System.out.println(N - 1);
}
/* Notice if the discriminant is zero, then necessarily
N = 4 * K. In an alternate form, the quadratic will be
written as i^2 - Ni + N^2/4, also recognized as (i - N/2)^2.
Therefore, the answer is N - 2. */
else if (N*N - 4*N*K == 0)
{
System.out.println(N - 1);
}
/* Test if discriminant is positive. If it is, the number of
integers depends on the locus of solutions. */
else if(N*N - 4*N*K > 0)
{
long solnOne = (long) (N + Math.sqrt(N*N - 4*N*K))/2;
long solnTwo = (long) (N - Math.sqrt(N*N - 4*N*K))/2;
if(solnOne > solnTwo){
long temp = solnOne;
solnOne = solnTwo;
solnTwo = temp;
}
long LeftBound = (long) solnOne;
long RightBound = (long) solnTwo + 1;
/* Now there may be possibilities such that the left solution
is less than one and/or the right solution is greater than or equal
to the bound. We must account for these possibilities. */
/* Here, both bounds are unacceptable. Therefore, there is no
solution. */
if(LeftBound < 1 && RightBound >= N)
{
System.out.println(0);
}
/* Here, only the right bound is acceptable. */
else if(LeftBound < 1)
{
System.out.println(N - RightBound);
}
/* Here, only the left bound is acceptable. */
else if(RightBound >= N)
{
System.out.println(LeftBound);
}
/* Both bounds are allowed! */
else
{
System.out.println(N - RightBound + LeftBound);
}
}
}
}
}