6
\$\begingroup\$

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.

Write a function called isMaxMinEqual that accepts an integer array and returns 1 if the array is maxmin equal; otherwise it returns 0.

Note: Please do not use any string methods. No sorting allowed. No additional data structures including arrays allowed.

If the input array is:

{} | isMaxMinEqual should return 0 (array must have at least two different elements)

{2} | 0 (array must have at least two different elements)

{1, 1, 1, 1, 1, 1} | 0 (array must have at least two different elements)

{2, 4, 6, 8, 11} | 1 (Both max value (11) and min value 2 appear exactly one time)

{-2, -4, -6, -8, -11} | 1 (Both max value (-2) and min value -11 appear exactly one time).

Here is my solution. Is it possible to combine the loops?

    int max;
    int min;
    int maxCount = 0;
    int minCount = 0;
    if (a.length > 2) {
        max = a[0];
        min = a[0];
    } else {
        return 0;
    }
    for (int num : a) {
        if (num > max) {
            max = num;
        }
        if (num < min) {
            min = num;
        }
    }
    for (int num : a) {
        if (max == num) {
            maxCount++;
        }
        if (min == num) {
            minCount++;
        }
    }
    if (maxCount == minCount) {
        return 1;
    }
    return 0;
\$\endgroup\$
4
\$\begingroup\$

Reviews:

  • Nomenclature "a" : Name doesn't reflect the motive.
  • ">2" is incorrect. It should be ">1".
  • {1, 1, 1, 1, 1, 1} will be incorrect in your case as min and max are same.
  • a could be null so we should check

    if(input == null || input.size <2){
        return 0;
    }
    
  • Multiple returns, though debatable but advisable.
  • Coming to reducing the loops:

    for (int num : a) {
        if (num > max) {
            max = num;
            maxCount = 1;
        } else if (num == max) {
            maxCount ++;
        }
    
        if (num < min) {
            min = num;
            minCount = 1;
        } else if (num == min) {
            minCount ++;
        }
    }
    
  • If you wish to minimize further: return (max != min && maxCount == minCount)

\$\endgroup\$
  • \$\begingroup\$ Though I am tempted to give you +1 for mentioning the case of 1, 1, 1, 1, 1, 1, I am also tempted to give you a -1 because of the "Multiple returns" article. \$\endgroup\$ – Simon Forsberg Jan 22 '17 at 17:21
  • \$\begingroup\$ My bad. I didn't check the heading of the article. \$\endgroup\$ – thepace Jan 23 '17 at 4:28
  • \$\begingroup\$ Personally I don't see anything bad with multiple returns at all. \$\endgroup\$ – Simon Forsberg Jan 23 '17 at 7:31
2
\$\begingroup\$

The key to only looping once is here:

for (int num : a) {
    if (num > max) {
        max = num;
    }
    if (num < min) {
        min = num;
    }
}

You can add a if (num == max) and if (num == min) and increase the maxCount and minCount there. And then you reset maxCount and minCount to 1 whenever you change the max or min.

for (int num : a) {
    if (num > max) {
        max = num;
        maxCount = 1;
    }
    if (num < min) {
        min = num;
        minCount = 1;
    }
    if (max == num) {
        maxCount++;
    }
    if (min == num) {
        minCount++;
    }
}

Other suggestions:

  • Return a boolean true/false instead of an integer 1 and 0.
  • In the if (a.length > 2) check you will return the wrong result for an array such as [2, 10] which as far as I understand it correctly should return true.
  • Use the name input instead of a.
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.