// Alejandro Santillana /* * given an array of int values, * is there a index in the array where the sum of values[0 .. index] * is equal to (or balances) the sum of values[index+1 .. length-1] * look at the examples below * Your task: design and code the canBalance methods that returns true or false * the program should be efficient. * can you solve the problem without using nested loops? 1 loop? 2 loops? * Then run the program to see if your method works correctly. */ public class CanBalance { public static void main(String[] args) { boolean r; r = canBalance(new int[] {1, 1, 1, 2, 1}); System.out.println("canBalance( [1, 1, 1, 2, 1] ) should be true. You returned "+r); r = canBalance(new int[] {2, 1, 1, 2, 1 }); System.out.println("canBalance( [2, 1, 1, 2, 1]) should be false. You returned "+r); r = canBalance(new int[] {10, 10}); System.out.println("canBalance( [10,10]) should be true. You returned "+r); r = canBalance(new int[] {10, 0, 1, -1, 10} ); System.out.println("canBalance([10, 0, 1, -1, 10]) should be true. You returned "+r); r = canBalance(new int[] { 1}); System.out.println(" should be false. You returned "+r); r = canBalance(new int[] {2, 1, 1, 1, 1}); System.out.println("canBalance([2, 1, 1, 1, 1]) should be true. You returned "+r); } public static boolean canBalance(int[] a) { int sum_a = 0; int sub_b = 0; for(int i =0;i