Check if the number is balanced | Practice | GeeksforGeeks

Given an integer N which has odd number of digits, find whether the given number is a balanced or not. 
An odd digit number is called a balanced number if the sum of all digits to the left of the middle digit and the sum of all digits to the right of the middle digit is equal.
Example 1:
Input:
N = 1234006
Output: 1 
Explanation: Total number of digits in
N = 7. Middle digit is 4. LHS = 1+2+3 = 6
and RHS = 0+0+6 = 6. LHS is equal to RHS. 

Example 2:
Input:
N = 12345
Output: 0
Explanation: LHS = 1+2 = 3 and RHS = 4+5
= 9. LHS and RHS are not equal.

Your Task:  
You dont need to read input or print anything. Complete the function balancedNumber() which takes string N as input parameter and returns a boolean value denoting if N is balanced or not.
Note: The driver code prints 1 if the returned value is true otherwise 0

Expected Time Complexity: O(number of digits in N)
Expected Auxiliary Space: O(1)

Constraints:
1<= N <= 10100
N will always have odd number of digits.
Since N can be very large, it is given as a string. 
 
0
ashutosh0366tripathi2 weeks ago
class Solution:def balancedNumber(self, N):    s=str(N)    middle=(len(s)//2)    c=0    d=0    for i in range(0,middle):        c+=int(s[i])    for j in range(middle+1,len(s)):        d+=int(s[j])       if c==d:        return 1    else:        return 0
0
codewithshoaib193 weeks ago
Boolean balancedNumber(String N)   {       // your code here        long ls=0;       long rs=0;       for(int i=0;i<N.length()/2;i++){           ls+=Long.parseLong(N.substring(i,i+1));       }       for(int i=(N.length()/2)+1;i<N.length();i++){           rs+=Long.parseLong(N.substring(i,i+1));       }       return ls==rs;   }
0
amitrrrvaa00033 weeks ago
Python:
s = str(N)k = len(s)//2+1s1=0s2=0for i in range(len(s)):     if i<k-1:         s1 += int(s[i])     elif i==k-1:         continue     else:         s2 += int(s[i])if s1==s2:     return 1else:     return 0
+1
singhpiyush81282 months ago
bool balancedNumber(string N){    // code here    int sum1=0,sum2=0;    for(int i=0;i<N.size()/2;i++)     { sum1+= N[i]-'0';         sum2+= N[N.size()-1-i]-'0';     }   return sum1==sum2;}
0
priyansh708902 months ago
  int mid;    int length = N.length();    mid = length/2;    int sum1 = 0;    int sum2 = 0;    for(int i = 0;i<mid;i++)    {        sum1 = sum1 + int(N[i]);    }    for(int i = mid+1 ;i<length;i++)    {        sum2 = sum2 + int(N[i]);    }    if(sum1 == sum2)    {        return true;    }    else    {        return false;    }}
+1
codeman993 months ago
bool balancedNumber(string N){
	    // code here
	    int sm1=0,sm2=0;
	    int mid=N.size()/2;
	    for(int i=0;i<mid;i++){
	        sm1+=N[i]-'0';
	    }
	    for(int i=mid+1;i<N.size();i++){
	        sm2+=N[i]-'0';
	    }
	    return sm1==sm2;
	}
0
plavanya9673 months ago
class Sol{   Boolean balancedNumber(String N)   {            int s=0;            int e=0;            int n=N.length();            for(int i=0;i<n/2;i++){                s+=N.charAt(i)-'0';            }            for(int i=n-1;i>n/2;i--){                e+=N.charAt(i)-'0';            }            return s==e;   }}
0
dhirunand3 months ago
class Sol
{
    Boolean balancedNumber(String N)
    {
        int left= 0, right = 0;
        for(int i = 0; i<N.length()/2; i++){
            left+=N.charAt(i) - '0';
            right+=N.charAt(i+1+N.length()/2) - '0';
        }
        
        return left==right;
    }
}
0
minturajmdb19993 months ago
 bool balancedNumber(string N){       int left=0,right=0;    int mid=N.size()/2;    for(int i=0;i<mid;i++)     {         left=left+(int(N[i])-48);              }     for(int i=mid+1;i<N.size();i++)      right=right+(int(N[i])-48);       return left==right ?1:0;}
0
sudhirswain1203 months ago
class Solution:def balancedNumber(self, N): # your code here n=str(N) mid=len(N)//2 lhs=sum(map(int,n[:mid])) rhs=sum(map(int,n[mid+1:])) return lhs==rhs
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.