Hammelkeule
Hammelkeule

Reputation: 317

Max Value Except One Number

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.

Upvotes: 0

Views: 631

Answers (5)

Abhishek Singh
Abhishek Singh

Reputation: 9188

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;

Upvotes: 0

Pavneet_Singh
Pavneet_Singh

Reputation: 37404

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.

Upvotes: 2

Patrick Parker
Patrick Parker

Reputation: 4959

    int requirements[] = { 6, 1, 2, 3, 4, 5 };
    int max = Arrays.stream(requirements).filter(i -> i!=6).max().getAsInt();
    System.out.println(max);

Upvotes: 0

davidxxx
davidxxx

Reputation: 131346

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;

Upvotes: 3

Ramha krishna
Ramha krishna

Reputation: 53

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

    }

}

Upvotes: 0

Related Questions