codingBat can balance code example
Example 1: can balance coding bat solution
public boolean canBalance(int[] nums) {
int a= nums.length;
int b=nums[0];
int flag=0;
if (a>2) {
for (int i = 1; i < nums.length; i++) {
b = b + nums[i];
b = b;
int sum = 0;
for (int j = i + 1; j < nums.length; j++) {
sum = sum + nums[j];
}
if (sum == b) {
flag=1;
break;
}
else {
flag=0;
}
}
}
else if (nums.length==2){
int q=nums[0];
int w=nums[1];
if (q==w){
flag=1;
}
else{
flag=0;
}
}
else{
flag=0;
}
if (flag==0){
return false;
}
else{
return true;
}
}
// Thank you
Example 2: codingBat can balance
public boolean canBalance(int[] nums) {
boolean isEqual = false;
int index = 0;
int sumOfElements = 0;
int sum;
//overarching
for(int i = 0; i < nums.length; i++) {
sum = 0;
//add all the elements besides the first element
for(int k = index + 1; k < nums.length; k++) {
sum = sum + nums[k];
}
sumOfElements = sumOfElements + nums[i];
index++;
//we will compare if nums[i] == sum
if(sumOfElements == sum) {
isEqual = true;
}
}
return isEqual;
}