0

I want to find out the maximum value of an array which holds numbers between 0 - 6. But the number 6 should be excluded. In my logic the 5 is the highest number, so I have to sort out the 6.

for (int i = 0; i < requirements.length; i++) {
    for (int k = i + 1; k < requirements.length; k++) {
        if(requirements[i] < requirements[k])
                && requirements[k] != 6) {
            highest = requirements[k];
        } else {
            if(requirements[i] != 6) {
                highestAsilLevel = requirements[i]; 
            }
        }
    }
}

I got this far, but this won't work for any case.

AxelH
  • 14,325
  • 2
  • 25
  • 55
Hammelkeule
  • 197
  • 5
  • 17

5 Answers5

3

Why two loops ? Just one should be enough :

Integer max = null;
for (int i = 0; i < requirements.length; i++) {
     Integer currentValue = requirements[i];
     if (currentValue!=6 && (max==null || currentValue>max)){
           max = currentValue;
     }
 }
 return max;
davidxxx
  • 125,838
  • 23
  • 214
  • 215
  • Careful, `&&` takes priority over `||`, so you'll have a NPE if the first value of the array is a 6 (see [here](https://ideone.com/aOCVzc)). You want to add parenthesis around that `max==null || currentValue>max`. Good answer otherwise, I like that you used `Integer` so that an array that is empty or full of 6 will return `null`. – Aaron Nov 28 '16 at 10:51
  • You are right. I should put the `OR` condition between parentheses to avoid that. Thank you. Done – davidxxx Nov 28 '16 at 10:59
2

1.) Sort your array

2.) start loop from the end

3.) compare number if it's not 6 then you got your number

int num=-1;
Arrays.sort(array);
for(int i=array.length-1;i>=0;i--){
    if(array[i]!=6){
       num = array[i];
       break;
    }
}

if(num!=-1){
    // you found your value
}else{
 //  all 6 , no expected value found  
}

Note : For future readers Array.sort guarantee n*log(n) time complexity which is considerably efficient especially when array size is not huge Read this and this beautiful article for further details.

Community
  • 1
  • 1
Pavneet_Singh
  • 36,884
  • 5
  • 53
  • 68
0

I think you want second highest number in array.. you can do the following

public class TwoMaxNumbers {
public static void main(String a[]){
    int requirements[] = {5,34,78,2,45,1,99,23};
    int maxOne = 0;
    int maxTwo = 0;
    for(int n:requirements){
        if(maxOne < n){
            maxTwo = maxOne;
            maxOne =n;
        } else if(maxTwo < n){
            maxTwo = n;
        }
    }
    System.out.println("Second Max Number: "+maxTwo);
}
}

if you want to avoid only number 6 you can do this

int max = null;
for (int i = 0; i < requirements.length; i++) {
 int max1 = requirements[i];
 if ((max1!=6) && (max==null || max1>max)){
       max = max1;
   }
}
return max;
Abhishek Singh
  • 9,008
  • 5
  • 28
  • 53
  • Looking at OP's code, it looks like he's not trying to get the second highest value but rather the highest value that isn't 6. – Aaron Nov 28 '16 at 10:37
  • I don't know, I jumped at the same conclusion as you at the beginning, probably because of the title and because it is a more common problem, but the description makes it pretty clear he just wants to avoid 6. – Aaron Nov 28 '16 at 10:41
  • I would say that if this is not clear, we should ask for more information first. – AxelH Nov 28 '16 at 10:49
0

Here is your ans:

public class Test {
    public static void main(String[] args) {

        int num[] = { 6, 1, 2, 3, 4, 5 };
        int max = 0;
        for (int i = 0; i < num.length; i++) {
            if (max < num[i] && num[i] != 6) {
                max = num[i];
            }
        }
        System.out.println(max);

    }

}
  • While this works in this specific case because the range of numbers is [0-6], I would advise against initializing `max` to 0 : if by the future the array could hold negative numbers, this code would fail when handling arrays containing only negative values. The best practice would be to initialize `max` to the first value in the array and iterating over the rest, the second best would be to initialize `max` to `Integer.MIN_VALUE`. In this second case (and your case too) you have to be aware that the max of an empty array will be that specific value (which might or might not be a good thing) – Aaron Nov 28 '16 at 10:46
  • What is the differences with **davidxxx** answer ? Except that his will works with the full Integer range. – AxelH Nov 28 '16 at 10:52
  • Hum ... you're welcome ? I am telling you this is already an answer, so that this doesn't provide anything. And that this is not fully functionnal. But ok – AxelH Nov 28 '16 at 11:03
0
    int requirements[] = { 6, 1, 2, 3, 4, 5 };
    int max = Arrays.stream(requirements).filter(i -> i!=6).max().getAsInt();
    System.out.println(max);
Patrick Parker
  • 4,863
  • 4
  • 19
  • 51