An array is defined to be maxmin equal if it contains at least two different elements and the number of times the maximum value occur is the same as the number of times the minimum value occur. So {11, 4, 9, 11, 8, 5 , 4, 10} is maxmin equal, because the max value 11 and min value 4 both appear two times in the array.
- if the input array is
{}
,isMaxMinEqual
should returnfalse
(array must have at least two different elements) status = false; - if the input array is
{2}
,isMaxMinEqual
should returnfalse
(array must have at least two different elements). - if the input array is
{1,1,1,1,1,1}
,isMaxMinEqual
should returnfalse
(array must have at least two different elements). - if the input array is
{2,4,6,8,11}
,isMaxMinEqual
should returntrue
(Both max value(11) and min value 2 appear exactly one time). - if the input array is
{-2,-4,-6,-8,-11}
,isMaxMinEqual
should returntrue
(Both max value(-2) and min value -11 appear exaclty one time).
I wrote the class MaxMin
which satisfied the above condition.
public class MaxMin {
public static void main(String args[]) {
System.out.println("The result is: " + isMaxMinEqual(new int[]{2, 2}));
}
public static boolean isMaxMinEqual(int[] a) {
boolean status = false;
int count = 0;
if (a.length < 2) {
status = false;
return status;
}
for (int i = 1; i < a.length; i++) {
if (a[i - 1] != a[i]) {
count++;
}
}
int max = a[0];
int min = a[0];
for (int i = 0; i < a.length; i++) {
if (a[i] > max) {
max = a[i];
} else if (a[i] < min) {
min = a[i];
}
}
int maxCount = 0;
for (int j = 0; j < a.length; j++) {
if (a[j] == max) {
maxCount++;
}
}
int minCount = 0;
for (int j = 0; j < a.length; j++) {
if (a[j] == min) {
minCount++;
}
}
System.out.println(count);
System.out.println("max is: " + max);
System.out.println("min is: " + min);
System.out.println("Max Count is: " + maxCount);
System.out.println("Min Count is: " + minCount);
if (maxCount == minCount && count >= 1) {
status = true;
}
return status;
}
}