Are you interested in becoming an AWS Certified Solutions Architect?
Discover a new interactive way of training for the exam.
Become a Premium Member and unlock a new, free course in leading technologies each month.
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//i thought since 1+1 is not equal to 2+1 it should be false?
canBalance([2, 1, 1, 2, 1]) → false//i thought since 2+1 is equal to 2+1 it should be true??
canBalance([10, 10]) → true
Add your voice to the tech community where 5M+ people just like you are talking about what matters.
But, they want us to split the array into two pieces
front and back
break at what index or point? just randomly we can break?
public boolean canBalance(int[] nums) {
for (int i = 0; i < nums.length; i++) {
int leftTotal=0;
leftTotal=leftTotal+nums[i];
for (int j = nums.length-1; j >i; j--) {
int rightTotal=0;
rightTotal=rightTotal+nums[i];
if(rightTotal==leftTotal){
return true;
}
}
}
return false;
}
Expected Run
canBalance([1, 1, 1, 2, 1]) → true true OK
canBalance([2, 1, 1, 2, 1]) → false true X
canBalance([10, 10]) → true true OK
canBalance([10, 0, 1, -1, 10]) → true true OK
canBalance([1, 1, 1, 1, 4]) → true true OK
canBalance([2, 1, 1, 1, 4]) → false true X
canBalance([2, 3, 4, 1, 2]) → false true X
canBalance([1, 2, 3, 1, 0, 2, 3]) → true true OK
canBalance([1, 2, 3, 1, 0, 1, 3]) → false true X
canBalance([1]) → false false OK
canBalance([1, 1, 1, 2, 1]) → true true OK
other tests
X
public class CanBalance {
public static void main(String[] args) {
// TODO Auto-generated method stub
int[] ar={1,1,1,2,1};
System.out.println("canBalance value is--->"+canBalance(ar));
}
public static boolean canBalance(int[] nums) {
for (int i = 0; i < nums.length; i++) {
int leftTotal=0;
leftTotal=leftTotal+nums[i];
for (int j = nums.length-1; j >i; j--) {
int rightTotal=0;
rightTotal=rightTotal+nums[i];
if(leftTotal==rightTotal){
return true;
}
}
}
return false;
}
}
public class CanBalance {
public static void main(String[] args) {
// TODO Auto-generated method stub
int[] ar={1,1,1,2,1};
System.out.println("canBalance value is--->"+canBalance(ar));
}
public static boolean canBalance(int[] nums) {
for (int i = 0; i < nums.length; i++) {
int leftTotal=0;
leftTotal=leftTotal+nums[i];
System.out.println("i is " + i + " leftTotal is " + leftTotal);
for (int j = nums.length-1; j >i; j--) {
int rightTotal=0;
rightTotal=rightTotal+nums[i];
System.out.println("j is " + j + " rightTotal is " + leftTotal);
if(leftTotal==rightTotal){
return true;
}
}
}
return false;
}
}
The output isrightTotal=rightTotal+nums[i];
is wrong. rightTotal should have equal to 3(at that point). You have i where you should have j.
public boolean canBalance(int[] nums) {
int leftTotal=0;
int rightTotal=0;
for (int i = 0; i < nums.length; i++) {
leftTotal=leftTotal+nums[i];
// System.out.println("i is " + i + " leftTotal is " + leftTotal);
for (int j = nums.length-1; j >i; j--) {
rightTotal=rightTotal+nums[j];
// System.out.println("j is " + j + " rightTotal is " + rightTotal);
if(leftTotal==rightTotal){
return true;
}
}
}
return false;
}
Expected Run
canBalance([1, 1, 1, 2, 1]) → true true OK
canBalance([2, 1, 1, 2, 1]) → false false OK
canBalance([10, 10]) → true true OK
canBalance([10, 0, 1, -1, 10]) → true true OK
canBalance([1, 1, 1, 1, 4]) → true false X
canBalance([2, 1, 1, 1, 4]) → false false OK
canBalance([2, 3, 4, 1, 2]) → false true X
canBalance([1, 2, 3, 1, 0, 2, 3]) → true false X
canBalance([1, 2, 3, 1, 0, 1, 3]) → false false OK
canBalance([1]) → false false OK
canBalance([1, 1, 1, 2, 1]) → true true OK
other tests
OK
public class CanBalance {
public static void main(String[] args) {
// TODO Auto-generated method stub
//int[] ar={1,1,1,2,1};
//System.out.println("canBalance value is--->"+canBalance(ar));
//2, 1, 1, 2, 1
int[] ar1={1, 2, 3, 1, 0, 1, 3};
System.out.println("canBalance value is--->"+canBalance(ar1));
}
public static boolean canBalance(int[] nums) {
int leftTotal=0;
int rightTotal=0;
for (int i = 0; i < nums.length; i++) {
leftTotal=leftTotal+nums[i];
System.out.println("i is " + i + " leftTotal is " + leftTotal);
for (int j = nums.length-1; j >i; j--) {
rightTotal=rightTotal+nums[j];
System.out.println("j is " + j + " rightTotal is " + rightTotal);
if(leftTotal==rightTotal){
return true;
}
}
}
return false;
}
}
My solution has nested for loops . Something like
for( whole array){
for( front){ add them}
for( back){add them}
if front equals back return true
}
int leftTotal=0;
int rightTotal=0;
public boolean canBalance(int[] nums) {
int leftTotal=0;
for (int i = 0; i < nums.length; i++) {
leftTotal=leftTotal+nums[i];
// System.out.println("i is " + i + " leftTotal is " + leftTotal);
int rightTotal=0;
for (int j = nums.length-1; j >i; j--) {
rightTotal=rightTotal+nums[j];
//System.out.println("j is " + j + " rightTotal is " + rightTotal);
}
if(leftTotal==rightTotal){
return true;
}
}
return false;
}
int rightTotal=0;
for (int j = nums.length-1; j >i; j--) {
do i need to define ....I don't know. I can't really understand your code(thinking). My thoughts :
public class CanBalance {
public static void main(String[] args) {
int[] ar1={2, 1, 1, 2, 1};
System.out.println("canBalance value is--->"+canBalance(ar1));
}
public static boolean canBalance(int[] nums) {
int leftTotal=0;
for (int i = 0; i < nums.length; i++) {
leftTotal=leftTotal+nums[i];
System.out.println("i is " + i + " leftTotal is " + leftTotal);
int rightTotal=0;
for (int j = nums.length-1; j >i; j--) {
rightTotal=rightTotal+nums[j];
System.out.println("j is " + j + " rightTotal is " + rightTotal);
}
if(leftTotal==rightTotal){
return true;
}
}
return false;
}
}
i is 0 leftTotal is 2any improvements, modifications, refinement to above code? how does your code looks like?
j is 4 rightTotal is 1
j is 3 rightTotal is 3
j is 2 rightTotal is 4
j is 1 rightTotal is 5
i is 1 leftTotal is 3
j is 4 rightTotal is 1
j is 3 rightTotal is 3
j is 2 rightTotal is 4
i is 2 leftTotal is 4
j is 4 rightTotal is 1
j is 3 rightTotal is 3
i is 3 leftTotal is 6
j is 4 rightTotal is 1
i is 4 leftTotal is 7
canBalance value is--->false
YOu need to test for equality after each time you add the right value.
public class CanBalance {
public static void main(String[] args) {
int[] ar={4, 2, 3, 3};
System.out.println("canBalance value is--->"+canBalance(ar));
}
public static boolean canBalance(int[] nums) {
boolean result = false;
int front = 0;
int back = 0;
for(int i = 0; i < nums.length; i++){
for(int f = 0; f < i + 1 ; f++){
front += nums[f];
System.out.println("f is " + f + " front is " + front);
}
for(int b = i + 1; b < nums.length ; b++){
back += nums[b];
System.out.println("b is " + b + " back is " + back);
}
if(front == back){
result = true;
break;
}
else{
front = 0;
back = 0;
}
}
return result;
}
}
Output was Only difference with your code is where you test for equality
public boolean canBalance(int[] nums) {
int leftTotal=0;
int rightTotal=0;
for (int i = 0; i < nums.length; i++) {
leftTotal=leftTotal+nums[i];
// System.out.println("i is " + i + " leftTotal is " + leftTotal);
// int rightTotal=0;
for (int j = nums.length-1; j >i; j--) {
rightTotal=rightTotal+nums[j];
//System.out.println("j is " + j + " rightTotal is " + rightTotal);
}
if(leftTotal==rightTotal){
return true;
}
}
return false;
}
Expected Run
canBalance([1, 1, 1, 2, 1]) → true false X
canBalance([2, 1, 1, 2, 1]) → false false OK
canBalance([10, 10]) → true true OK
canBalance([10, 0, 1, -1, 10]) → true true OK
canBalance([1, 1, 1, 1, 4]) → true false X
canBalance([2, 1, 1, 1, 4]) → false false OK
canBalance([2, 3, 4, 1, 2]) → false false OK
canBalance([1, 2, 3, 1, 0, 2, 3]) → true false X
canBalance([1, 2, 3, 1, 0, 1, 3]) → false false OK
canBalance([1]) → false false OK
canBalance([1, 1, 1, 2, 1]) → true false X
other tests
OK
int rightTotal=0;
for (int i = 0; i < nums.length; i++) {
why above code fails some tests if i move rightSum initialization line to the top of outer for loop rather than inner for loop??because you are not resetting the total value.
i is 0 leftTotal is 2
j is 4 rightTotal is 1
j is 3 rightTotal is 3
j is 2 rightTotal is 4
j is 1 rightTotal is 5
i is 1 leftTotal is 3
j is 4 rightTotal is 1
j is 3 rightTotal is 3
j is 2 rightTotal is 4
i is 2 leftTotal is 4
j is 4 rightTotal is 1
j is 3 rightTotal is 3
i is 3 leftTotal is 6
j is 4 rightTotal is 1
i is 4 leftTotal is 7
canBalance value is--->false
why above code fails some tests if i move rightSum initialization line to the top of outer for loop rather than inner for loop??
because you are not resetting the total value.
If you are experiencing a similar issue, please ask a related question
Join the community of 500,000 technology professionals and ask your questions.