Find balanced array

Posted on

Problem

An array is called balanced if its even numbered elements (a[0], a[2], etc.) are even and its odd numbered elements (a[1], a[3], etc.) are odd. Write a function named isBalanced that accepts an array of integers and returns 1 if the array is balanced, otherwise it returns 0. Examples: {2, 3, 6, 7} is balanced since a[0] and a[2] are even, a[1] and a[3] are odd. {6, 7, 2, 3, 12} is balanced since a[0], a[2] and a[4] are even, a[1] and a[3] are odd. {7, 15, 2, 3} is not balanced since a[0] is odd. {16, 6, 2, 3} is not balanced since a[1] is even.
If you are programming in Java or C#, the function signature is
int isBalanced(int[ ] a)

I first try to check if an even index has odd value and if an odd index has even value, if the condition it yes then count variable value is changed to 0 otherwise it will not change the value of count variable and returns its default value.
Here is my solution. Is it possible to combine the loops?

public class inxedOddEven {
static int[] n = {0,3,2,5};
static int l = n.length;
public static void main(String[] arr){
    System.out.println(isBalanced(n));
}
public static int isBalanced(int[] a){
   int count = 1;
   for(int i = 0; i<l;i+=2){
       if(a[i]%2 != 0){
           count = 0;
           break;
       }
}
   for(int j = 1;j< l; j+=2 ){
       if(a[j]%2 == 0){
           count = 0;
           break;
       }
   }
   return count;
 }
}

Solution

Yes, it’s fairly trivial to combine the loops.

for (int i=0; i<l; i++) {
    if ((a[i] % 2) != (i % 2)) {
        count = 0;
        break;
    }
}

As for the code in general, just a few obvious points.

Names

Even knowing what the code is supposed to do, I’m not quite sure what inxedOddEven is supposed to mean. I guess the OddEven is sort of sensible, but I’m baffled at the meaning of inxed. I’d also tend to avoid l as a name. Depending on the font, it’s frequently easy to mistake for a 1.

Types

It seems to me that the obvious type for isBalanced to return would be a Boolean rather than an int.

Pure functions

I’d much rather see isBalanced written as a pure function that depends solely on its parameter. The current situation is rather confused–it receives an array as its parameter, but depends on the fact that l has been initialized to the size of that array before it’s been called.

Early returns

If you want to return a value from inside a loop, don’t be afraid to just return the value from inside the loop. Looking at things theoretically, it’s true that the result is no longer a “single entry, single exit” function, which some structured programming theorists considered an ideal. In reality, however, code like:

for (traverse array and check some condition)
    if (whatever)
        set condition to true
if (condition)
    return one value
else
    return another value

…tends to be fairly opaque compared to:

for (traverse array)
    if (some condition)
       return some value;
return other value;

Summary

Putting those together, our isBalanced might look something like this:

bool isBalanced(int[] inputArray) {
    for (int i=0; i<inputArray.length; i++)
        if ((inputArray[i] % 2) != (i % 2))
            return false;
    return true;
}

Leave a Reply

Your email address will not be published. Required fields are marked *