-
Code interface HighLow { static int[] findLargestAndSmallest(int[] nums) { if (isCollectionEmpty(nums)) return null; return findLargestAndSmallestGeneric(nums); } private static int[] findLargestAndSmallestGeneric(int[] nums) { int max = nums[0]; int min = nums[0]; for (int num : nums) { if (max < num) max = num; if (min > num) min = num; } return new int[]{min, max}; } private static boolean isCollectionEmpty(int[] nums) { return nums == null || nums.length == 0; } }
Test Cases import org.junit.Test; import static org.junit.Assert.*; import org.junit.runners.JUnit4; // TODO: Replace examples and use TDD by writing your own tests public class SolutionTest { int[] input = new int[]{-1,-2,-3,-4,-5,-6,-7,-8,-9,-1}; int[] output = new int[]{-9,-1}; public void allNegatives() { assertArrayEquals(output, HighLow.findLargestAndSmallest(input)); } int[] input2 = new int[]{1,-2,3,-4,-5,-6,-7,-8,-9}; int[] output2 = new int[]{-9,3}; public void test2() { assertArrayEquals(output2, HighLow.findLargestAndSmallest(input2)); } int[] input123 = new int[]{1,2,3}; int[] output123 = new int[]{1,3}; public void test21() { assertArrayEquals(output123, HighLow.findLargestAndSmallest(input123)); } int[] input3 = new int[]{4}; int[] output3 = new int[]{4,4}; public void oneItem() { assertArrayEquals(output3, HighLow.findLargestAndSmallest(input3)); } int[] input4 = new int[]{9,-1}; int[] output4 = new int[]{-1,9}; public void twoItems() { assertArrayEquals(output4, HighLow.findLargestAndSmallest(input4)); } int[] input5 = new int[]{}; int[] output5 = null; public void emptyArray() { assertArrayEquals(output5, HighLow.findLargestAndSmallest(input5)); } int[] input6 = null; int[] output6 = null; public void nullInput() { assertArrayEquals(output6, HighLow.findLargestAndSmallest(input6)); } }
Output:
-
Code - interface HighLow {
- static int[] findLargestAndSmallest(int[] nums) {
if (isCollectionEmpty(nums))return null;else if (hasOneElement(nums))return new int[]{nums[0], nums[0]};else if (hasTwoElements(nums))return sortTwoElements(nums);- if (isCollectionEmpty(nums)) return null;
- return findLargestAndSmallestGeneric(nums);
- }
- private static int[] findLargestAndSmallestGeneric(int[] nums) {
int max = Integer.MIN_VALUE;int min = Integer.MAX_VALUE;- int max = nums[0];
- int min = nums[0];
- for (int num : nums) {
if (max < num)max = num;if (min > num)min = num;- if (max < num) max = num;
- if (min > num) min = num;
- }
- return new int[]{min, max};
- }
static boolean hasOneElement(int[] nums) {return nums.length == 1;}static boolean hasTwoElements(int[] nums) {return nums.length == 2;}private static int[] sortTwoElements(int[] nums) {return (nums[0] <= nums[1])? new int[]{nums[0], nums[1]}: new int[]{nums[1], nums[0]};}- private static boolean isCollectionEmpty(int[] nums) {
- return nums == null || nums.length == 0;
- }
- }
- All
- {{group.name}} ({{group.count}})
This comment has been reported as {{ abuseKindText }}.
Show
This comment has been hidden. You can view it now .
This comment can not be viewed.
- |
- Reply
- Edit
- View Solution
- Expand 1 Reply Expand {{ comments?.length }} replies
- Collapse
- Remove
- Remove comment & replies
- Report
{{ fetchSolutionsError }}