-4

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

}

7 Answers7

4

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;
}
hosyvietanh
  • 286
  • 2
  • 11
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;
}
Codebender
  • 14,221
  • 7
  • 48
  • 85
1

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

Ankush soni
  • 1,439
  • 1
  • 15
  • 30
0

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;
}
Subhash Khimani
  • 427
  • 7
  • 22
  • Ok. For all the options that you guys have posted, I'm thankful, but this is what I mean about the cases that verify this code. These are the only 2 cases that worked fine: greatestNegative({1, 21, 23, -17, 7, -17, 14, 4, 5}) → -17 greatestNegative({9, 2, 3, 4, 7}) → 0 – Zuriel Del C. Cabrera Aug 10 '15 at 05:01
  • And here are some that are not working: greatestNegative({0, -9, -12, 6, 10, -15, 7}) → -9 (this one gives me -15 as an answer and it should be -9) – Zuriel Del C. Cabrera Aug 10 '15 at 05:03
0

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

Ahmad Al-Kurdi
  • 2,248
  • 3
  • 23
  • 39
0

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;

}
Somnath Kadam
  • 6,051
  • 6
  • 21
  • 37
0

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
Shar1er80
  • 9,001
  • 2
  • 20
  • 29