# Difference of Volumes of Cuboids in Java

## The challenge

In this simple exercise, you will create a program that will take two lists of integers, `a` and `b`. Each list will consist of 3 positive integers above 0, representing the dimensions of cuboids `a` and `b`. You must find the difference of the cuboids’ volumes regardless of which is bigger.

For example, if the parameters passed are `([2, 2, 3], [5, 4, 1])`, the volume of `a` is 12 and the volume of `b` is 20. Therefore, the function should return 8.

Your function will be tested with pre-made examples as well as random ones.

## The solution in Java code

Option 1:

```.wp-block-code{border:0;padding:0}.wp-block-code>div{overflow:auto}.shcb-language{border:0;clip:rect(1px,1px,1px,1px);-webkit-clip-path:inset(50%);clip-path:inset(50%);height:1px;margin:-1px;overflow:hidden;padding:0;position:absolute;width:1px;word-wrap:normal;word-break:normal}.hljs{box-sizing:border-box}.hljs.shcb-code-table{display:table;width:100%}.hljs.shcb-code-table>.shcb-loc{color:inherit;display:table-row;width:100%}.hljs.shcb-code-table .shcb-loc>span{display:table-cell}.wp-block-code code.hljs:not(.shcb-wrap-lines){white-space:pre}.wp-block-code code.hljs.shcb-wrap-lines{white-space:pre-wrap}.hljs.shcb-line-numbers{border-spacing:0;counter-reset:line}.hljs.shcb-line-numbers>.shcb-loc{counter-increment:line}.hljs.shcb-line-numbers .shcb-loc>span{padding-left:.75em}.hljs.shcb-line-numbers .shcb-loc::before{border-right:1px solid #ddd;content:counter(line);display:table-cell;padding:0 .75em;text-align:right;-webkit-user-select:none;-moz-user-select:none;-ms-user-select:none;user-select:none;white-space:nowrap;width:1%}```interface CuboidVolumes {
static int findDifference(int[] a, int[] b) {
return Math.abs(a[0] * a[1] * a[2] - b[0] * b[1] * b[2]);
}
}
```Code language: Java (java)```

Option 2:

``````public class CuboidVolumes {
public static int findDifference(final int[] firstCuboid, final int[] secondCuboid) {
int vol1 = 1, vol2 = 1;
for (int i = 0; i < 3; i++) {
vol1 *= firstCuboid[i];
vol2 *= secondCuboid[i];
}

return Math.abs(vol1 - vol2);
}
}
```Code language: Java (java)```

Option 3:

``````public class CuboidVolumes {
public static int findDifference(final int[] firstCuboid, final int[] secondCuboid) {
if (getMulti(firstCuboid) > getMulti(secondCuboid)) return getMulti(firstCuboid) - getMulti(secondCuboid);
return getMulti(secondCuboid) - getMulti(firstCuboid);
}

public static int getMulti(int[] arr) {
int sum = 1;
for (int i = 0; i < arr.length; i++) {
sum *= arr[i];
}
return sum;
}
}
```Code language: Java (java)```

## Test cases to validate our solution

``````import org.junit.Test;
import static org.junit.Assert.assertEquals;
import org.junit.runners.JUnit4;

public class SolutionTest {
@Test
public void sampleTestCases() {
assertEquals(14, CuboidVolumes.findDifference(new int[]{3, 2, 5}, new int[]{1, 4, 4}));
assertEquals(106, CuboidVolumes.findDifference(new int[]{9, 7, 2}, new int[]{5, 2, 2}));
assertEquals(30, CuboidVolumes.findDifference(new int[]{11, 2, 5}, new int[]{1, 10, 8}));
assertEquals(31, CuboidVolumes.findDifference(new int[]{4, 4, 7}, new int[]{3, 9, 3}));
assertEquals(0, CuboidVolumes.findDifference(new int[]{15, 20, 25}, new int[]{10, 30, 25}));
}
}
```Code language: Java (java)```
Tags:
Subscribe
Notify of