Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions balanced.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/'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. '/

#include<bits/stdc++.h>
using namespace std;

class Solution{
public:
bool balancedNumber(string N)
{
// code here
if(N.size() % 2 == 0){return false;}
int mid = N.size()/2;
int sum1 = 0,sum2 = 0;
for(int i=0;i<mid;i++){
sum1 += N[i]-'0';
}
for(int i=mid+1;i<N.size();i++){
sum2 += N[i]-'0';
}
if(sum1 == sum2){return true;}
else{return false;}
}
};

// { Driver Code Starts.
int main(){
int t;
cin>>t;
while(t--)
{
string N;
cin>>N;
Solution ob;
cout<<ob.balancedNumber(N)<<endl;
}
return 0;
}