Question

I want to tell if an array of integers is alternating. in JAVA.

For example:

a[]={1,-1,1,-1,1,-1}  --> true
a[]={-1,1,-1,1,-1}    --> true
a[]={1,-4,1-6,1}      --> true
a[]={1,1,1,14,5,3,2}  --> false

I have started to write some code that uses flags. For example if the current_is_positive=0 and else = 1, but I'm not getting anywhere. What is a good way to achieve this effect?

Was it helpful?

Solution

I think you mean alternating in sign, i.e. positive number, negative number, positive number, etc.? You could use the following strategy:

Skip the first element.

For every other element, compare its sign with the sign of the previous element:

  • If they're different, the sequence is still alternating upto now - you should continue.
  • If they're the same sign, the sequence is not alternating. You can stop processing at this point.

As this sounds like a homework assignment, I'll leave it upto you to write the appropriate code in Java.

OTHER TIPS

Here my solution:

This checks that element n+1 is the inverse of the element n.

    public static void main(String[] args) {
        int[] ints = {1, -1, 2, -1};
        System.out.println(new Example().isArrayAlternating(ints));
    }

    public boolean isArrayAlternating(int[] ints) {
        if (ints == null || ints.length % 2 != 0) {
            return false;
        }
        for (int i = 0; i < ints.length - 1; i++) {
            if (ints[i] != ints[i + 1]*(-1)) {
                return false;
            }
        }
        return true;
    }

If you only wanted to check for positive number, negative number...n times, without paying attention to value:

public static void main(String[] args) {
    int[] ints = {1, -1, 2, -1};
    System.out.println(new Example().isArrayAlternating(ints));
}

public boolean isArrayAlternating(int[] ints) {
    if (ints == null || ints.length % 2 != 0) {
        return false;
    }
    for (int i = 0; i < ints.length - 1; i++) {
        if (ints[i] >= 0 && ints[i + 1] >= 0 || ints[i] <= 0 && ints[i + 1] <= 0) {
            return false;
        }
    }
    return true;
}

You can simply check if all items are equal to the item two steps back. I don't know what language you are using, but for example using C# you could do it like this:

bool alternating = a.Skip(2).Where((n, i) => a[i] == n).Count() == a.Length - 2;

Edit:

So you want to check if the sign of the values are alternating, not the values?

Then just check the sign against the previous item:

bool alternating = a.Skip(1).Where((n,i) => Math.Sign(n) == -Math.Sign(a[i])).Count() == a.Length-1;
boolean prevPositive = arr[0] > 0, error = false;
for (int i = 1; i < arr.length; ++i) {
    boolean current = arr[i] > 0;
    if (current != prevPositive) {
        current = prevPositive;
    } else {
        error = true;
        break;
    }
}
if (error)
   System.out.println("No");
else
   System.out.println("Yes");
private enum SIGN {
    POSITIVE, NEGATIVE
};

public static boolean isAlternating(int... ints) {
    SIGN last = null;
    for (int i : ints) {
        if (i >= 0) {
            if (last == null) {
                last = SIGN.POSITIVE;
            } else {
                if (last == SIGN.POSITIVE) {
                    return false;
                } else {
                    last = SIGN.POSITIVE;
                }
            }
        } else {
            if (last == null) {
                last = SIGN.NEGATIVE;
            } else {
                if (last == SIGN.NEGATIVE) {
                    return false;
                } else {
                    last = SIGN.NEGATIVE;
                }
            }
        }
    }
    return true;
}

Run through the array from index 1 till the end.

At each index, evaluate (a[i] > 0) == (a[i-1] > 0). If this is true, then your array is not alternating.

If you make it till the end without concluding it is not alternating, then it is alternating :)

  • Run a loop, from first index to maximum possible with a step of 2, to check for same sign.
  • Then again a loop, from 2nd index to maximum possible with a step of 2, to check for opposite sign, but all same.
  • So, loop from index - 0, 2, 4, 6, ...
  • then loop from index - 1, 3, 5, 7, ...

  • Then check that the multiplication of every number in both loop with the first number in that iteration should be positive.

    int a[]={1,-1,1,-1,1,-1};
    
    boolean alternating = true;
    
    for (int i = 0; i < a.length; i = i + 2) {
        if (a[i] * a[0] > 0) {
        } else {
            alternating = false;
        }
    }
    for (int i = 1; i < a.length; i = i + 2) {
        if (a[i] * a[1] > 0) {
        } else {
            alternating = false;
        }
    }
    
    if (alternating) {
        System.out.println("Array is alternating");
    } else 
        System.out.println("Array is not alternating");
    }
    
For i = 2 to n

   check whether A[i-1] && A[i] are with diff sign..
   in C++; return  ((A[i-1] ^  A[i]) < 0).  

Same explained here : http://www.youtube.com/watch?v=Z59REm2YKX0

EDIT

If an integer is negative, then the high order bit is 1. Otherwise, it's 0. You can check if two integers have different signs by XORing them together. If the signs are different, then the high order bit of the result will be 1. If they're the same, then the high order bit will be 0. Thus,

A XOR B < 0 is equivalent to "A and B have different signs"

Peter Ruderman

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top