Ad

Please make sure the test you submit is at least compiling

Code
Diff
  • public class MaxConsecutiveSum{
      public static int[] maxConsecutiveSum(int num){
        return new int[0];
      }
    }
    • public class maxConsecutiveSum{
    • public class MaxConsecutiveSum{
    • public static int[] maxConsecutiveSum(int num){
    • //code goes here
    • return [];
    • return new int[0];
    • }
    • }

It's a bot of a personal thing but I think this makes it easier to read and
understand what is going on at each step.
The tests were all using the same output and input from the first test.

Code
Diff
  • 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);
    
            return findLargestAndSmallestGeneric(nums);
        }
    
        private static int[] findLargestAndSmallestGeneric(int[] nums) {
            int max = Integer.MIN_VALUE;
            int min = Integer.MAX_VALUE;
    
            for (int num : nums) {
                if (max < num)
                    max = num;
                else 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;
        }
    }
    • 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};
    • }
    • 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);
    • return findLargestAndSmallestGeneric(nums);
    • }
    • private static int[] findLargestAndSmallestGeneric(int[] nums) {
    • int max = Integer.MIN_VALUE;
    • int min = Integer.MAX_VALUE;
    • for (int num : nums) {
    • if (max < num)
    • max = num;
    • else 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;
    • }
    • }