While I was doing the code to pass the roots of quadratic equation using an array list, I encountered the wrong answer for the given test case 752 904 164
. Its answer was -1,-1
, but my answer was 0,0
, so I tried searching for the solution and found I have to use the Math.floor()
function, and after using that function, my test case runs successfully.
public ArrayList<Integer> quadraticRoots(int a, int b, int c) {
// code here
ArrayList alist = new ArrayList();
double root1 ,root2 ;
double d = (b*b)-(4*a*c);
if (d<0)
{
root1 = (-b+Math.sqrt(d))/2*a;
alist.add(-1);
return alist;
}
else
{
double srt= Math.sqrt(d);
root1 = (-b+srt)/(2*a);
root2 = (-b-srt)/(2*a);
alist.add((int)Math.floor(Math.max(root1,root2))); //
alist.add((int)Math.floor(Math.min(root1,root2)));
return alist;
}
}