# Subtraction of Fractionsby Isai Damier, Android Engineer @ Google

```/***************************************************************************
*  Author: Isai Damier
* Title: Subtraction of Fractions
* Project: geekviewpoint
* Package: algorithms
*
* Statement:
*   Subtract the two given positive fractions
*
* Sample Input: {5,6}, {7,8})
* Sample Output: {-1,24}
*
* Technical Details:
*   This is the same algorithm you learned in grade school:
*   1) find the lowest common denominator, which is the same as finding
*     the lowest common multiplier of the given denominators.
*   2) Convert the given fractions into equivalent fractions with the
*     new denominator
*   3) subtract the numerators
*   4) reduce the resulting fraction using the GCD (greatest common
*     divisor) of its numerator and denominator.
*
**************************************************************************/
public int[] fractionSubtraction(int[] a, int[] b) {
int denominator = LCM(a, b);
int numA = denominator / a * a;
int numB = denominator / b * b;
int numerator = numA - numB;
int gcd = GCD(0 > numerator? -numerator:numerator, denominator);
gcd = 0 > gcd ? -gcd : gcd;
return new int[]{numerator / gcd, denominator / gcd};
}```
```import org.junit.Test;
import static org.junit.Assert.*;

public class NumbersTest {

/**
* Test of fractionSubtraction method, of class Numbers.
*/
@Test
public void testFractionSubtraction() {
System.out.println("fractionSubtraction");
Numbers numbers = new Numbers();
int[] expResult = {-1,24};
int[] result = numbers.fractionSubtraction(new int[] {5,6}, new int[] {7,8});
assertArrayEquals(expResult, result);
result = numbers.fractionSubtraction(new int[] {5,6}, new int[] {6,8});
assertArrayEquals(new int[] {1,12}, result);
}
}```