## The challenge

Your job is to find the gravitational force between two spherical objects (obj1 , obj2).

### Two arrays are give :

• `arr_val` (value array), consists of 3 elements
• 1st element : mass of obj 1
• 2nd element : mass of obj 2
• 3rd element : distance between their centers
• `arr_unit` (unit array), consists of 3 elements
• 1st element : unit for mass of obj 1
• 2nd element : unit for mass of obj 2
• 3rd element : unit for distance between their centers

### Mass units are :

• kilogram (kg)
• gram (g)
• milligram (mg)
• microgram (μg)
• pound (lb)

### Distance units are :

• meter (m)
• centimeter (cm)
• millimeter (mm)
• micrometer (μm)
• feet (ft)

value of G = 6.67 x 10-11N.kg–2.m2

1ft = 0.3048m

1lb = 0.453592kg

return value must be Newton for force (obviously)

## Test cases

 `````` 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 `````` ``````import static org.junit.Assert.assertEquals; import java.util.concurrent.ThreadLocalRandom; import org.junit.Test; public class TestCases { private static void test(double expected, double[] arrVal, String[] arrUnit) { assertEquals(expected, Solution.solution(arrVal, arrUnit), expected * 1e-5); } @Test public void fixedTests() { test(6.67e-12, new double[] {1000, 1000, 100}, new String[] {"g", "kg", "m"}); test(6.67e-9, new double[] {1000, 1000, 100}, new String[] {"kg", "kg", "m"}); test(0.0000667, new double[] {1000, 1000, 100}, new String[] {"kg", "kg", "cm"}); } private static final String[] WEIGHT_UNITS = {"kg", "g", "mg", "μg", "lb"}; private static final double[] WEIGHT_UNIT_CONVERSION_FACTORS = {1, 1e-3, 1e-6, 1e-9, 0.453592}; private static final String[] DISTANCE_UNITS = {"m", "cm", "mm", "μm", "ft"}; private static final double[] DISTANCE_UNIT_CONVERSION_FACTORS = {1, 1e-2, 1e-3, 1e-6, 0.3048}; @Test public void randomTests() { ThreadLocalRandom random = ThreadLocalRandom.current(); for (int i = 0; i < 100; i++) { double m1 = random.nextDouble(1, 1e5); double m2 = random.nextDouble(1, 1e5); double r = random.nextDouble(0.3, 1e7); double[] arrVal = new double[] {m1, m2, r}; double f = 6.67e-11 * m1 * m2 / r / r; int u1 = random.nextInt(WEIGHT_UNITS.length); int u2 = random.nextInt(WEIGHT_UNITS.length); int u3 = random.nextInt(DISTANCE_UNITS.length); arrVal /= WEIGHT_UNIT_CONVERSION_FACTORS[u1]; arrVal /= WEIGHT_UNIT_CONVERSION_FACTORS[u2]; arrVal /= DISTANCE_UNIT_CONVERSION_FACTORS[u3]; String[] arrUnit = new String[] {WEIGHT_UNITS[u1], WEIGHT_UNITS[u2], DISTANCE_UNITS[u3]}; test(f, arrVal, arrUnit); } } } ``````

## The solution in Java

Option 1:

 `````` 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 `````` ``````public class Solution { public static double solution(double[] arrVal, String[] arrUnit) { double newtons = 6.67e-11 * arrVal * arrVal / (arrVal * arrVal); for (String el : arrUnit) { newtons /= (el=="g") ? 1e3 : (el=="mg") ? 1e6 : (el=="μg") ? 1e9 : (el=="ft") ? 0.3048 * 0.3048 : 1; newtons *= (el=="lb") ? 0.453592 : (el=="cm") ? 1e4 : (el=="mm") ? 1e6 : (el=="μm") ? 1e12 : 1; } return newtons; } } ``````

Option 2:

 `````` 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 `````` ``````import java.util.*; public class Solution { public static double solution(double[] arrVal, String[] arrUnit) { MapmassUnits = new HashMap<>(); massUnits.put("kg",1d); massUnits.put("g",1/1000d); massUnits.put("mg",1/1000000d); massUnits.put("μg",1/1000000000d); massUnits.put("lb",0.453592); Mapdistancenits = new HashMap<>(); distancenits.put("m",1d); distancenits.put("cm",1/100d); distancenits.put("mm",1/1000d); distancenits.put("μm",1/1000000d); distancenits.put("ft",0.3048); return 6.67e-11*(arrVal*massUnits.get(arrUnit)*arrVal*massUnits.get(arrUnit))/(arrVal*arrVal*distancenits.get(arrUnit)*distancenits.get(arrUnit)); } } ``````