what is the logic behind this question can anyone explain me please

Given a non-empty array, return true if there is a place to split the array so that the sum of the numbers on one side is equal to the sum of the numbers on the other side.

canBalance([1, 1, 1, 2, 1]) → true
canBalance([2, 1, 1, 2, 1]) → false
canBalance([10, 10]) → true
gnanagowthaman sankarAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

☠ MASQ ☠Commented:
consider placing "+" between each number
then replace "+" with "="
if the sum works the answer is "True"
Now put the "+" back and move the "=" to the space between the next digits
Keep doing that until you reach the end of the string
If at any point the sum is valid your answer is "True" if there are no valid sums the answer is "False"

canBalance([1, 1, 1, 2, 1]) → true

1=1+1+2+1 = False
1+1=1+2+1 = False
1+1+1=2+1 = True
1+1+1+2=1 = False

So for 1,1,1,2,1 overall the answer is "True"

canBalance([2, 1, 1, 2, 1]) → false
No  combination of "+" and "=" produces a valid sum

canBalance([10, 10]) → true

10=10 = True

etc...
1

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
gnanagowthaman sankarAuthor Commented:
thank you sir @☠ MASQ ☠
0
☠ MASQ ☠Commented:
0
Starting with Angular 5

Learn the essential features and functions of the popular JavaScript framework for building mobile, desktop and web applications.

krakatoaCommented:
You can have a look at this too :

//Using ia1 as array here, but you can change it for demo purposes to other chosen test array values

class SplitTest{

 public static void main(String[] args){

  
  int[] ia1 = {2, 1, 1, 2, 1};
  
  int total=0;
  int accum = 0;
  boolean breakout=false;

  for(int i : ia1){total+=i;}
  
  System.out.println("Total is "+total);
  
  for(int y=0;y<ia1.length;y++){
  
    accum+=ia1[y];
    if((total-accum)==accum){
        System.out.println("Split possible at index "+y);
        breakout=true;
        break;
    }
    
  }
  
  if (breakout==false){System.out.println("No Split possible");}
    
 }

}

/*
canBalance([1, 1, 1, 2, 1]) → true
canBalance([2, 1, 1, 2, 1]) → false
canBalance([10, 10]) → true

*/

Open in new window

1
gnanagowthaman sankarAuthor Commented:
	int temp = 0;
		int j_temp = 0;
		int i_temp = 0;
		boolean flag = false;
		int[] x = new int[nums.length];
		for (int j = nums.length - 1; j >= 0; j--) {
			x[j] = temp + nums[j];
		}
		for (int i = 0; i < nums.length; i++) {
			i_temp = i_temp + x[i];
			for (int j = i + 1; j < nums.length; j++) {
				j_temp = j_temp + nums[j];

			}
			if (i_temp == j_temp) {
				flag = true;
				break;
			}
			j_temp = 0;
		}
		return flag;

Open in new window

My source code krakatoa, Thanks for your code krakatoa
0
krakatoaCommented:
And does your code work? I imagine it does . . . although I have not run it.  But look at how complicated yours algo is. The meta to this problem is that you only need to see whether an accumulated total is equal to the entire array value minus that accumulated total.

So as the actual canBalance code, it would be :

public boolean canBalance(int[] nums) { 

  int total=0;
  int accum = 0;
  
  for(int i : nums){total+=i;}
  
  for(int y=0;y<nums.length;y++){
  
    accum+=nums[y];
    if((total-accum)==accum){
        
        return true;
        
    }
    
  }
  
  return false;

}

Open in new window

0
gnanagowthaman sankarAuthor Commented:
Yes it works for all conditions. Though am a beginner in future i will try  to code short. thanks you so much krakatoa.
1
krakatoaCommented:
ok.
0
krakatoaCommented:
thanks you so much krakatoa.

So much you don’t have enough points to say it

Thanks and goodbye.
0
gnanagowthaman sankarAuthor Commented:
Sorry sir mistake is done by me I closed via mobile. Sorry sir. I will correct in future sir please sorry.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.