Array is Balanced Array or Not A balanced array is defined to be an array where for every value n in the array, -n also is in the array.


{-2, 3, 2, -3} is a balanced array.
So is {-2, 2, 2, 2}. But {-5, 2, -2} is not because 5 is not in the array.


I wrote the following class BalancedArray which satisfied above condition. 

public class BalancedArray {
public static void main(String args[]) {
    System.out.println("The result is: " + isBalanced(new int[]{-2, 3, 2, -3}));
}
public static boolean isBalanced(int[] a){
    boolean status = false;
    for(int i = 0; i &lt; a.length; i++) {
        if(a[i] &gt; 0) {
            for(int j = i+1; j &lt; a.length; j++) {
                if(a[i] == Math.abs(a[j])) {
                    status  =  true;

                }
            }

        } else if(a[i] &lt; 0) {
            for(int k = i+1; k &lt; a.length; k++) {
                if(Math.abs(a[i]) == a[k]) {
                    status = true;

                }
            }


        }
        System.out.println(status);
      if(status) {
          status= true;
      } else {
          status  = false;
          break;
      }
    }

    return status;
}
}


Is this proper way to check Balanced array or I am missing some conditions to check in array??
An alternative way could be using a map, the average lookup time should be O(1) so this algorithm would be O(n) on average.

public static boolean isBalanced(int[] a) 
{     
    Map&lt;Integer, Integer&gt; map = new HashMap&lt;&gt;();  
    for(int number : a)
    {   
        int key = Math.abs(number);
        Integer value = map.get(key);

        if(value == null)
            map.put(key, number);
        else if (value != number)
            map.put(key, 0);
    }   
    for (int v : map.values())
    {
        if(v != 0) return false;
    }

    return true;
}


A more readable version in java 8 ( Autoboxing and unboxing may hurt the performance a lot thought.)

 public static boolean isBalanced(int[] a) 
 {


       Map&lt;Integer, Integer&gt; map = Arrays.stream(a)
                                         .boxed()
                                         .collect(Collectors.toMap(
                                                 Math::abs,
                                                 Function.identity(),
                                                 (x, y) -&gt; x.equals(y)? x: 0 )
                                         );
        return map.values()
                  .stream()
                  .allMatch(p -&gt; p == 0);
 }