Wednesday, February 3, 2016

Root of Equation

Root of Equation

Given an equation: ax2 + bx + c = 0. Find the root of the equation.
  • If there are two roots, return a list with two roots in it.
  • If there are only one root, return a list with only one root in it.
  • If there are no root for the given equation, return an empty list.

Example
Given a = 1, b = -2, c = 1. return [1].
Given a = 1, b = -3, c = 2. return [1, 2]. The first one should smaller than the second.
Given a = 1, b = 1, c = 1. return [].



Solution

delta < 0 no root
delta == 0 only one root
delta > 0 two roots, compare them and return 


public class Solution {
    /**
     * @param a, b, c: parameters of the equation
     * @return a double array, contains at most two root
     */
    public double[] rootOfEquation(double a, double b, double c) {
        // Write your code here
        double delta = b * b - 4 * a * c;
        
        if (delta < 0) {
            return new double[0];
        }
        
        if (delta == 0) {
            double[] root = new double[1];
            root[0] = (-b / (2 * a));
            return root;
        }
        
        double[] root = new double[2];
        root[0] = (-b + Math.sqrt(delta)) / (2 * a);
        root[1] = (-b - Math.sqrt(delta)) / (2 * a);
        
        if (root[0] > root[1]) {
            double tmp = root[0];
            root[0] = root[1];
            root[1] = tmp;
        }
        
        return root;
    }
}