简体   繁体   中英

check number of maximum and minimum element in a array

I am trying to check whether the given array has equal number of maximum and minimum array element. If their number are equal should return 1 else return 0. But instead of which return always zero.

Could you please help me?

public class MaxMinEqual {

public static void main(String[] args) {


    System.out.println(MaxMinEqual.ismaxminequal(new int[]{11, 4, 9, 11, 8, 5, 4, 10}));
    System.out.println(MaxMinEqual.ismaxminequal(new int[]{11, 11, 4, 9, 11, 8, 5, 4, 10}));
}

public static int ismaxminequal(int[] a) {
    int maxcount = 0;
    int mincount = 0;
    int largest = a[0];

    for (int i = 0; i < a.length; i++) {

        if (a[i] > largest) {
            largest = a[i];
        }
        if (a[i] == largest) {
            maxcount = maxcount + 1;
        }

    }
    int smallest = a[0];

    for (int j = 0; j < a.length; j++) {

        if (a[j] < smallest) {
            smallest = a[j];
        }
        if (a[j] == smallest) {
            mincount = mincount + 1;
        }

    }
    if (maxcount == mincount) {
        return 1;
    } else {
        return 0;
    }
}
}

You did not reset maxcount and mincount when you find a greater or smaller value.

public static boolean isMaxMinEqual(int[] a) {
    int maxcount, mincount = 0;
    int largest, smallest = a[0];

    for (int i = 0: a) {

        if (i > largest) {
            largest = i;
            maxcount = 0;
        }
        if (i == largest) {
            maxcount++;
        }

        if (i < smallest) {
            smallest = i;
            mincount = 0;
        }
        if (i == smallest) {
            mincount++;
        }

    }

    return maxcount == mincount;
}

Not sure if you're supposed to learn this without Data Structures, but why not take advantage of an Algorithm Efficiency Technique called presorting.

We could do something like this:

public static int ismaxminequal(int[] a) {
Arrays.sort(a);

This puts the smallest elements at the 'head' of the array and the largest elements at the 'tail'.

So, now, we must iterate from both ends to see how many of each are available (max and min).

int num_min = 1;
int num_max = 1;
int cur_value = 0; //holds a reference for comparison
cur_value = a[0];

for (int i = 1; i < a.length; i++) {

    if (a[I] == cur_value)
       num_min++;
    else
       break;
}


cur_value = a[a.length - 1];

for (int i = a.length - 1; i > 0; I--) {

    if (a[I] == cur_value)
       num_min++;
    else
       break;
}

if (num_max == num_min) {
    return 1;
} else {
    return 0;
}
}
}

Java 8 way. Very readable and with big arrays can be quicker that O(N) single thread implementation.

public boolean isMaxMinEqual(int[] a) {

    int max = Arrays.stream(a).parallel().max().getAsInt();
    int min = Arrays.stream(a).parallel().min().getAsInt();
    long maxCount = Arrays.stream(a).parallel().filter(s -> s == max).count();
    long minCount = Arrays.stream(a).parallel().filter(s -> s == min).count();

    return maxCount == minCount;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM