We help IT Professionals succeed at work.

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

gnanagowthaman sankar
on
163 Views
Last Modified: 2017-12-25
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
Comment
Watch Question

CERTIFIED EXPERT
Most Valuable Expert 2013
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION
thank you sir @☠ MASQ ☠
CERTIFIED EXPERT
Most Valuable Expert 2013

Commented:
No problem

How do I close my question?
CERTIFIED EXPERT

Commented:
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

	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
CERTIFIED EXPERT

Commented:
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

Yes it works for all conditions. Though am a beginner in future i will try  to code short. thanks you so much krakatoa.
CERTIFIED EXPERT

Commented:
ok.
CERTIFIED EXPERT

Commented:
thanks you so much krakatoa.

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

Thanks and goodbye.
Sorry sir mistake is done by me I closed via mobile. Sorry sir. I will correct in future sir please sorry.

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.