import java.util.*; interface HighLow { static int[] printLargestAndSmallest(int[] nums) { if(nums == null || nums.length == 0) return null; else if(nums.length == 1) return new int[]{nums[0], nums[0]}; else if(nums.length == 2) return (nums[0] <= nums[1]) ? new int[]{nums[0], nums[1]} : new int[]{nums[1], nums[0]}; int max=Integer.MIN_VALUE,min=Integer.MAX_VALUE; for(int i=0;i<nums.length; i++){ if(max<nums[i]) max = nums [i]; else if(min>nums[i]) min=nums[i]; } return new int[]{min, max}; } }
- import java.util.*;
- interface HighLow {
- static int[] printLargestAndSmallest(int[] nums) {
int max=Integer.MIN_VALUE,min=Integer.MAX_VALUE;for(int i=0;i<nums.length; i++){if(max<nums[i]) {max = nums [i];}else if(min>nums[i]){min=nums[i];}- if(nums == null || nums.length == 0)
- return null;
- else if(nums.length == 1)
- return new int[]{nums[0], nums[0]};
- else if(nums.length == 2)
- return (nums[0] <= nums[1]) ? new int[]{nums[0], nums[1]} : new int[]{nums[1], nums[0]};
- int max=Integer.MIN_VALUE,min=Integer.MAX_VALUE;
- for(int i=0;i<nums.length; i++){
- if(max<nums[i])
- max = nums [i];
- else if(min>nums[i])
- min=nums[i];
- }
return nums == null || nums.length == 0 ? null : new int[]{min, max};- return new int[]{min, max};
- }
- }
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}; @Test public void allNegatives() { assertArrayEquals(output, HighLow.printLargestAndSmallest(input)); } int[] input2 = new int[]{1,-2,3,-4,-5,-6,-7,-8,-9}; int[] output2 = new int[]{-9,3}; @Test public void test2() { assertArrayEquals(output, HighLow.printLargestAndSmallest(input)); } int[] input3 = new int[]{4}; int[] output3 = new int[]{4,4}; @Test public void oneItem() { assertArrayEquals(output, HighLow.printLargestAndSmallest(input)); } int[] input4 = new int[]{9,-1}; int[] output4 = new int[]{-1,9}; @Test public void twoItems() { assertArrayEquals(output, HighLow.printLargestAndSmallest(input)); } int[] input5 = new int[]{}; int[] output5 = null; @Test public void emptyArray() { assertArrayEquals(output, HighLow.printLargestAndSmallest(input)); } int[] input6 = null; int[] output6 = null; @Test public void nullInput() { assertArrayEquals(output, HighLow.printLargestAndSmallest(input)); } }
- 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};
- @Test
- public void allNegatives() {
- assertArrayEquals(output, HighLow.printLargestAndSmallest(input));
- }
- int[] input2 = new int[]{1,-2,3,-4,-5,-6,-7,-8,-9};
- int[] output2 = new int[]{-9,3};
- @Test
- public void test2() {
- assertArrayEquals(output, HighLow.printLargestAndSmallest(input));
- }
- int[] input3 = new int[]{4};
- int[] output3 = new int[]{4,4};
- @Test
- public void oneItem() {
- assertArrayEquals(output, HighLow.printLargestAndSmallest(input));
- }
- int[] input4 = new int[]{9,-1};
- int[] output4 = new int[]{-1,9};
- @Test
- public void twoItems() {
- assertArrayEquals(output, HighLow.printLargestAndSmallest(input));
- }
- int[] input5 = new int[]{};
- int[] output5 = null;
- @Test
- public void emptyArray() {
- assertArrayEquals(output, HighLow.printLargestAndSmallest(input));
- }
- int[] input6 = null;
- int[] output6 = null;
- @Test
- public void nullInput() {
- assertArrayEquals(output, HighLow.printLargestAndSmallest(input));
- }
- }