How do I find the largest negative value in an array with both positive and negative values?

前端 未结 7 1218
無奈伤痛
無奈伤痛 2021-01-29 15:43

I need to return the greatest negative value, and if there are no negative values, I need to return zero. Here is what I have:

public int greatestNegative(int[]          


        
相关标签:
7条回答
  • 2021-01-29 16:09

    If you need the greatest negative number then sort array thin search for first negative number

    import java.util.Arrays;
    
    class Main {
    
        public static void main(String[] args) {
            int arr[] = { 2, 4, 1, 7,2,-3,5,-20,-4,5,-9};
            System.out.println(greatestNegative(arr));
        }
    
        private static int greatestNegative(int[] arr) {
            Arrays.sort(arr);
            for (int i = arr.length - 1; i >= 0; i--) {
                if (isNegative (arr[i])) {
                    return arr[i];
                }
            }
            return 0;
        }
    
        private static boolean isNegative (int i) {
            return i < 0;
        }
    }
    

    Output : -3

    0 讨论(0)
  • 2021-01-29 16:13

    You have to try this....

    public int greatestNegative(int[] list) {
        int negNum = 0;
        for(int i=0; i<list.length; i++) {
            if(list[i] < negNum){
                negNum = list[i];
            }
        }
        return negNum;
    }
    
    
    public int largNegative(int[] list) {
        int negNum = 0;
        boolean foundNeg = false;
        for(int i=0; i<list.length; i++) {
            if(list[i] < negNum && !foundNeg){
                foundNeg = true;
                negNum = list[i];
            } else if(foundNeg && list[i] < 0 && negNum < list[i]) {
                negNum = list[i];
            }
        }
        return negNum;
    }
    
    0 讨论(0)
  • 2021-01-29 16:14

    Start by setting your "maxNegative" value to 0. Then assign the first negative number you come across. After that, only assign negative numbers that are higher. If there are no negative numbers, then your "maxNegative" will still be zero.

    public static void main(String[] args) {
        int arr[] = {2, -1, 4, 1, 0, 7, 2, -3, 5, 9, -4, 5, -9};
        int maxNegative = 0;
        for (int i = 0; i < arr.length; i++) {
            if (maxNegative == 0 && arr[i] < maxNegative) {
                // Set the first negative number you come across
                maxNegative = arr[i];
            } else if (maxNegative < arr[i] && arr[i] < 0) {
                // Set greater negative numbers
                maxNegative = arr[i];
            }
        }
        System.out.println(maxNegative);
    }
    

    Results:

    -1
    

    Java 8

    Then there are streams, that allow you to do this with one line of code.

    public static void main(String[] args) {
        int arr[] = {2, 4, 1, 0, 7, 2, -3, 5, 9, -4, 5, -9};
        int maxNegative = Arrays.stream(arr).filter(a -> a < 0).max().orElse(0);
        System.out.println(maxNegative);
    }
    

    Results:

    -3
    
    0 讨论(0)
  • 2021-01-29 16:19

    You just need to think of this problem as 2 steps:

    1. Only consider negative values in list[].
    2. In the loop within negative values, update current result if (result == 0) or (value > result).

    Code:

    public int greatestNegative(int[] list) {
        int result = 0;
        for (int i = 0; i < list.length; i++) {
            if (list[i] < 0) {
                if (result == 0 || list[i] > result) {
                    result = list[i];
                }
            }
        }
        return result;
    }
    
    0 讨论(0)
  • 2021-01-29 16:22

    Please check following code, which will first calculate small number from array, then check is it positive? if yes return 0 else return negative.

    public static int greatestNegative(int[] list) 
    {
        int negativeNumbers = Integer.MAX_VALUE;
        for (int i = 0; i < list.length; i++) {
            if (list[i] < negativeNumbers)
                    negativeNumbers  = list[i];
        }
    
        if(negativeNumbers  >=0)
             return 0;
        else
             return negativeNumbers;
    
    }
    
    0 讨论(0)
  • 2021-01-29 16:26

    Here is the code which return the smallest negative number

    public static int greatestNegative(int[] list) {
            int negativeNumbers = 0;
            for (int i = 0; i < list.length; i++) {
               if (list[i] < 0 && list[i] < negativeNumbers)
                   negativeNumbers  = list[i];
            }
    
            return negativeNumbers;
        }
    

    Input : 1, 2, -3, 5, 0, -6

    Output : -6

    Input : 1, 2, 3, 5, 0, 6

    Output : 0

    0 讨论(0)
提交回复
热议问题