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

六眼飞鱼酱① 提交于 2019-12-04 07:37:27

问题


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[] list) {


    for (int i = 0; i < list.length; i++) {


       if (list[i] < 0)
           negativeNumbers ++;
    }

    int j = list.length - 1;

    while (j >= 0) {
       if (list[j - negativeNumbers] < 0) {
        list[j] = 0;
        list[j - 1] = list[j - negativeNumbers];
        negativeNumbers--;
        j--;
       }
       else{
        list[j] = list[j - negativeNumbers];
        j--;
     }
  }

}

回答1:


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;
}



回答2:


Just go about finding the max number with an added condition.

public static int greatestNegative(int[] list) {
    int max = Integer.MIN;
    boolean set = false;
    for (int i = 0; i < list.length; i++) {
        if (list[i] < 0 && list[i] > max) {
             max = arr[i];
             set = true;
        }
    }
    if (!set)
        max = 0;
    return max;
}



回答3:


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




回答4:


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




回答5:


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;

}



回答6:


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;
}



回答7:


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


来源:https://stackoverflow.com/questions/31911769/how-do-i-find-the-largest-negative-value-in-an-array-with-both-positive-and-nega

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!