May Sohatchevzzki
May Sohatchevzzki

Reputation: 25

Need help in java algorithm for finding max and min

I need to write a java algorithm that receive 5 numbers from type int and print the max and min by using only 6 times with the Conditional Exchange:

If (x>y) {
    Int tmp = x;
    x=y;
    y=x;
} 

The problem is that I only manage to do this algorithm with 7 conditional exchange and not 6. Can someone help me to understand what I'm missing?

    Scanner myScanner = new Scanner(System.in);
    int a = myScanner.nextInt();
    int b = myScanner.nextInt();
    int c = myScanner.nextInt();
    int d = myScanner.nextInt();
    int e = myScanner.nextInt();

    if(a>b)
    {
        int tmp = b;
        b = a;
        a = tmp;
    }

    if(a>c)
    {
        int tmp = c;
        c = a;
        a = tmp;
    }

    if(a>d)
    {
        int tmp = d;
        d = a;
        a = tmp;
    }

    if(a>e)
    {
        int tmp = e;
        e = a;
        a = tmp;
    }

    if(b>e)
    {
        int tmp = e;
        e = b;
        b = tmp;
    }

    if(c>e)
    {
        int tmp = e;
        e = c;
        c = tmp;
    }

    if(d>e)
    {
        int tmp = e;
        e = d;
        d = tmp;
    }

    System.out.println(a);
    System.out.println(e);

Upvotes: 0

Views: 146

Answers (4)

MrCode
MrCode

Reputation: 16

// Compare your numbers in pairs, I've paired up a-b and c-d and e will be compared separately

Scanner myScanner = new Scanner(System.in);

int a = myScanner.nextInt();
int b = myScanner.nextInt();
int c = myScanner.nextInt();
int d = myScanner.nextInt();
int e = myScanner.nextInt(); 

int min ; int max ;
int min1 = a, max1 = b;
int min2 = c, max2 = d ; 

if(a > b ){
      min1 = b ;
      max1 = a ;
      // Here I set original min and max to min1 and max2 to save extra conditionals
      min = min1 ; 
      max = max1 ;  
}

if(c > d ){
      min2 = b ;
      max2 = a ; 
}

if(min1 > min2)
      min = min2 ; 
if(max2 > max1)
      max = max2 ;

// One last comparison for e and we've found what were looking for :)
if( e > max){ 
      max = e ; }
if( e < min){
      min = e ; }

Upvotes: 0

raven1981
raven1981

Reputation: 1445

Comparing two pairs first, then you can save a comparison knowing than the first pair smaller must be compared with the second pair smaller and the first pair bigger must be compared with the second pair bigger. We have a number left that must be compared with the current smaller and bigger in case it fits there.

Implementation:

Scanner myScanner = new Scanner(System.in);
int a = myScanner.nextInt();
int b = myScanner.nextInt();
int c = myScanner.nextInt();
int d = myScanner.nextInt();
int e = myScanner.nextInt();

myScanner.close();

// First pair
if(a>e)
{
    int tmp = e;
    e = a;
    a = tmp;
}

// Second pair
if(c>d)
{
    int tmp = d;
    d = c;
    c = tmp;
}

// Smaller
if(a>c) {
    int tmp = a;
    a = c;
    c = tmp;
}

// Bigger
if(d>e) {
    int tmp = d;
    d = e;
    e = tmp;
}

// Remaining value bigger
if(b>e) {
    int tmp = b;
    b = e;
    e = tmp;
}

// Remaining value smaller     
if(a>b) {
    int tmp = b;
    b = a;
    a = tmp;
}



System.out.println(a);
System.out.println(e);

Upvotes: 0

Andy Turner
Andy Turner

Reputation: 140328

Think about it like a merge sort: break the problem down to finding the min and max for pairs of numbers; then for pairs of pairs etc.

if (a >= b) swap(a, b); // logically swap them; you can't actually write a method to do this.

Now a = min(a, b) and b = max(a, b).

if (c >= d) swap(c, d);

Now c = min(c, d) and d = max(c, d).

if (a >= c) swap(a, c);
if (b >= d) swap(b, d);

Now a = min(a, b, c, d) and d = max(a, b, c, d).

Then it's just a matter of handling e:

if (a >= e) swap(a, e);
if (d >= e) swap(d, e);

The min is in a; the max is in e.

Upvotes: 3

lifeisbeautiful
lifeisbeautiful

Reputation: 846

Put Every single element in an Array (arr1), And then import collections and Arrays

import java.util.Arrays;
import java.util.Collections; 

int min = Collections.min(Arrays.asList(arr1));  // arr1 will be your array
int max = Collections.max(Arrays.asList(arr1)); 

Upvotes: -1

Related Questions