patternjavaMinor
Array is MinMax Equal or Not
Viewed 0 times
arraynotequalminmax
Problem
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.
I wrote the class
- if the input array is
{},isMaxMinEqualshould returnfalse(array must have at least two different elements) status = false;
- if the input array is
{2},isMaxMinEqualshould returnfalse(array must have at least two different elements).
- if the input array is
{1,1,1,1,1,1},isMaxMinEqualshould returnfalse(array must have at least two different elements).
- if the input array is
{2,4,6,8,11},isMaxMinEqualshould returntrue(Both max value(11) and min value 2 appear exactly one time).
- if the input array is
{-2,-4,-6,-8,-11},isMaxMinEqualshould 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 max) {
max = a[i];
} else if (a[i] = 1) {
status = true;
}
return status;
}
}Solution
Varags
Since Java 1.5, you can use varargs in your method so that you can pass in a sequence of elements, instead of an explicit array:
Variables assignment
You have declared
'Debug' statements
These look like 'debug' statements where the developer (or user) is manually checking that the counting is done correctly. Usually, this should be done through a logging framework (so that the verbosity can be adjusted), or be eliminated entirely. It does not serve a direct purpose as to the method's usage.
Iterations
Your approach has to iterate through the array three times:
In fact, these can be done in one pass:
The idea here is that when you need to reset your min/max values, you can just reset your counters to
If you do not mind extra ternary operators, you can sort of combine both
Since Java 1.5, you can use varargs in your method so that you can pass in a sequence of elements, instead of an explicit array:
public static boolean isMaxMinEqual(int... values) {
// use values like a normal int[] array
}Variables assignment
You have declared
status right at the start, but it's actually redundant as you can simply return from the method directly in the places where you use them:if (a.length = 1) {
status = true;
}
return status;
*/
return maxCount == minCount && count >= 1; // no need to set status = true'Debug' statements
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);These look like 'debug' statements where the developer (or user) is manually checking that the counting is done correctly. Usually, this should be done through a logging framework (so that the verbosity can be adjusted), or be eliminated entirely. It does not serve a direct purpose as to the method's usage.
Iterations
Your approach has to iterate through the array three times:
- Find the min/max.
- Count the max.
- Count the min.
In fact, these can be done in one pass:
int min = Integer.MAX_VALUE;
int max = Integer.MIN_VALUE;
int minCounter = 0;
int maxCounter = 0;
for (int i : values) {
if (i max) {
max = i;
maxCounter = 1;
} else if (i == max) {
maxCounter++;
}
}
return minCounter == maxCounter && min != max;The idea here is that when you need to reset your min/max values, you can just reset your counters to
1 and proceed, instead of having to iterate again.If you do not mind extra ternary operators, you can sort of combine both
if branches per min/max too:int min = Integer.MAX_VALUE;
int max = Integer.MIN_VALUE;
int minCounter = 0;
int maxCounter = 0;
for (int i : values) {
if (i = max) {
maxCounter = 1 + (i == max ? maxCounter : 0);
max = i;
}
}
return minCounter == maxCounter && min != max;Code Snippets
public static boolean isMaxMinEqual(int... values) {
// use values like a normal int[] array
}if (a.length < 2) {
// status = false;
// return status;
return false; // shorter and clearer
}
// ...
/*
if (maxCount == minCount && count >= 1) {
status = true;
}
return status;
*/
return maxCount == minCount && count >= 1; // no need to set status = trueSystem.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);int min = Integer.MAX_VALUE;
int max = Integer.MIN_VALUE;
int minCounter = 0;
int maxCounter = 0;
for (int i : values) {
if (i < min) {
min = i;
minCounter = 1;
} else if (i == min) {
minCounter++;
}
if (i > max) {
max = i;
maxCounter = 1;
} else if (i == max) {
maxCounter++;
}
}
return minCounter == maxCounter && min != max;int min = Integer.MAX_VALUE;
int max = Integer.MIN_VALUE;
int minCounter = 0;
int maxCounter = 0;
for (int i : values) {
if (i <= min) {
minCounter = 1 + (i == min ? minCounter : 0);
min = i;
}
if (i >= max) {
maxCounter = 1 + (i == max ? maxCounter : 0);
max = i;
}
}
return minCounter == maxCounter && min != max;Context
StackExchange Code Review Q#117363, answer score: 3
Revisions (0)
No revisions yet.