-3

How to find the most significant number of negative integers in java? I have the following code and work well with positive integers. This is the code:

int max = 0; // not initialsing it also makes it 0
int[] n = {-1,-2,-3,-4};
for(int i = 0; i < n.lenght; i++)
   if(max < n[i]) 
      max = n[i];

Now, I can solve it by reducing the max variable to -5 or something below -4, but this is inefficient because we don't know the lowest value.

So, how can it be achieved?

Sambhav Khandelwal
  • 3,585
  • 2
  • 7
  • 38
  • 1
    No, but you know the lowest value an `int` can assume, that is [`Integer.MIN_VALUE`](https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/lang/Integer.html#MIN_VALUE) – Federico klez Culloca Jul 16 '22 at 14:06
  • 1
    If you know that the array contains at least one element, you can always initialize`max = n[0]`. ;-) – Seelenvirtuose Jul 16 '22 at 14:06
  • please add expected results, to make the question more clearer – rohitt Jul 16 '22 at 14:07
  • @rohitt the question is rather clear as it is. EDIT: I re-re-re-read it and it's rather confusing in language, you're right – Federico klez Culloca Jul 16 '22 at 14:08
  • Note: you're getting the _smallest_ value here, not the max/"most significant" value – OneCricketeer Jul 16 '22 at 14:18
  • 1
    @OneCricketeer No, the code that the OP posted is getting the max value, just like the question says. It just has the problem that it starts out with a max value of zero which is higher than the max value in the array – Erwin Bolwidt Jul 16 '22 at 14:31

2 Answers2

1

If i understood your question correctly, you are looking for the highest integer value within a list of integers.

You may use IntStreams to get the highest value of your data.

IntStream stream = IntStream.of(-9, -18, 54, 8, 7, 14, 3);
OptionalInt obj = stream.max(); // or use stream.min() for the lowest integer

if (obj.isPresent()) {
    System.out.println(obj.getAsInt()); // 54
}
else {
    System.out.println("stream is empty");
}

As @OneCricketeer suggested, can also use your existing int[] with streams:

int[] n = {-1, -2, -3, -4}; // your array of data

IntStream stream = Arrays.stream(n);
OptionalInt obj = stream.max();
if (obj.isPresent()) {
    System.out.println(obj.getAsInt()); // -1
} else {
    System.out.println("stream is empty");
}

Try it online!

wartoshika
  • 531
  • 3
  • 10
  • 1
    Can also use `Arrays.stream(n)` from the array in the question – OneCricketeer Jul 16 '22 at 14:20
  • Using streams and their terminal operations is actually a good advice here. – Seelenvirtuose Jul 16 '22 at 14:20
  • Creating a stream only to go through once is quite expensive – xthe_white_lionx Jul 16 '22 at 15:44
  • You should consider your concrete use case. There are reasons why use less code with the same result even if the performance part is not the best. Best example is a large project where maintainability is important. On the other hand when your code has to be performant your answer will work better. Most of the time i would prefer simpler solutions but it depends. – wartoshika Jul 18 '22 at 18:46
1
  1. The return type of your methode has to be the int wrapper class Integer, because you accept any values in the int array so the only way to show the user that the array doesn't have an max, which only could happen if the array is empty, is to return null.
  2. Now because your secure the case that the array could be empty the only other case is that the array has at least one element or more now you can initialize your start variable like this max = n[0] because if your array has only one element it has to be the greatest. For the other cases your loop will do the rest.

So there are two solutions first iterate by your own

private static Integer max(int[] numbers) {
    if (numbers.length != 0) {
        int max = numbers[0];
        for (int i = 1; i < numbers.length; i++)
            if (max < numbers[i]){
                max = numbers[i];
            }
        return max;
    }
    return null;
}

or second use only the API methodes (could be expansive)

private static Integer max(int[] numbers) {
    OptionalInt result = Arrays.stream(numbers).max();

    return result.isPresent() ? result.getAsInt() : null;
}