Check if the number is balanced
Last Updated :
24 Apr, 2023
Given a number N in the form of a string, the task is to check whether the given number is balanced or not.
Balanced Number: A number is said to be balanced if the sum of digits in the first half of it is equal to the sum of the digits in the second half.
Note: All Palindromic numbers are balanced numbers.
Examples:
Input: N = 19091
Output: Balanced
Explanation:
middle element is 0
Sum of left half = 1 + 9 = 10
Sum of right half = 9 + 1 = 10
Hence, the given number is a Balanced number.
Input: N = 133423
Output: Not Balanced
Explanation:
Sum of left half = 1 + 3 + 3 (7)
Sum of right half = 4 + 2 + 3 (9)
Hence, the given number is not Balanced
Approach:
Iterate over half the length of the number from the beginning. Calculate the sum of digits of the first half and the second half simultaneously by adding s[i] and s[number of digits – 1 – i] to leftSum and rightSum respectively. Finally, check if the leftSum and rightSum are equal or not.
Below is the implementation of the above approach.
C++
#include <bits/stdc++.h>
using namespace std;
void BalancedNumber(string s)
{
int Leftsum = 0;
int Rightsum = 0;
for ( int i = 0; i < s.size() / 2; i++) {
Leftsum += int (s[i] - '0' );
Rightsum += int (s[s.size() - 1 - i]
- '0' );
}
if (Leftsum == Rightsum)
cout << "Balanced" << endl;
else
cout << "Not Balanced" << endl;
}
int main()
{
string s = "12321" ;
BalancedNumber(s);
return 0;
}
|
Java
import java.io.*;
class GFG{
private static void BalancedNumber(String s)
{
int Leftsum = 0 ;
int Rightsum = 0 ;
for ( int i = 0 ; i < s.length() / 2 ; i++)
{
Leftsum += ( int )(s.charAt(i) - '0' );
Rightsum += ( int )(s.charAt(
s.length() - 1 - i) - '0' );
}
if (Leftsum == Rightsum)
System.out.println( "Balanced" );
else
System.out.println( "Not Balanced" );
}
public static void main (String[] args)
{
String s = "12321" ;
BalancedNumber(s);
}
}
|
Python3
def BalancedNumber(s):
Leftsum = 0
Rightsum = 0
for i in range ( 0 , int ( len (s) / 2 )):
Leftsum = Leftsum + int (s[i])
Rightsum = (Rightsum +
int (s[ len (s) - 1 - i]))
if (Leftsum = = Rightsum):
print ( "Balanced" , end = '\n' )
else :
print ( "Not Balanced" , end = '\n' )
s = "12321"
BalancedNumber(s)
|
C#
using System;
class GFG{
static void BalancedNumber( string s)
{
int Leftsum = 0;
int Rightsum = 0;
for ( int i = 0; i < s.Length / 2; i++)
{
Leftsum += ( int )(Char.GetNumericValue(s[i]) -
Char.GetNumericValue( '0' ));
Rightsum += ( int )(Char.GetNumericValue(s[s.Length -
1 - i]) -
Char.GetNumericValue( '0' ));
}
if (Leftsum == Rightsum)
Console.WriteLine( "Balanced" );
else
Console.WriteLine( "Not Balanced" );
}
static void Main()
{
string s = "12321" ;
BalancedNumber(s);
}
}
|
Javascript
<script>
function BalancedNumber(s)
{
let Leftsum = 0;
let Rightsum = 0;
for (let i = 0; i < s.length / 2; i++)
{
Leftsum += (s[i] - '0' );
Rightsum += (s[
s.length - 1 - i] - '0' );
}
if (Leftsum == Rightsum)
document.write( "Balanced" );
else
document.write( "Not Balanced" );
}
let s = "12321" ;
BalancedNumber(s);
</script>
|
Time Complexity: O(len), where len is the length of the string.
Auxiliary Space: O(1)
Approach 2 (Two Pointers) : The idea is to use 2 pointers from the starting index and the ending index and will check whether the number is palindromic in nature or not. To make it simple will convert the number to a string first and apply the described algorithm.
Below is the code for the implementation :
C++
#include <bits/stdc++.h>
using namespace std;
void BalancedNumber( int n)
{
string s = to_string(n);
int si = 0, ei = s.size() - 1;
bool flag = true ;
while (si < ei) {
if (s[si] == s[ei]) {
si++;
ei--;
}
else {
flag = false ;
break ;
}
}
if (flag)
cout << "Balanced" ;
else
cout << "Not Balanced" ;
}
int main()
{
int n = 1235321;
BalancedNumber(n);
return 0;
}
|
Java
public class BalancedNumber {
public static void isBalancedNumber( int n) {
String s = Integer.toString(n);
int si = 0 ;
int ei = s.length() - 1 ;
boolean flag = true ;
while (si < ei) {
if (s.charAt(si) == s.charAt(ei)) {
si++;
ei--;
}
else {
flag = false ;
break ;
}
}
if (flag) {
System.out.println( "Balanced" );
}
else {
System.out.println( "Not Balanced" );
}
}
public static void main(String[] args) {
int n = 1235321 ;
isBalancedNumber(n);
}
}
|
Python3
def is_balanced_number(n):
s = str (n)
si = 0
ei = len (s) - 1
flag = True
while si < ei:
if s[si] = = s[ei]:
si + = 1
ei - = 1
else :
flag = False
break
if flag:
print ( "Balanced" )
else :
print ( "Not Balanced" )
n = 1235321
is_balanced_number(n)
|
C#
using System;
public class Program
{
static void BalancedNumber( int n)
{
string s = n.ToString();
int si = 0, ei = s.Length - 1;
bool flag = true ;
while (si < ei)
{
if (s[si] == s[ei])
{
si++;
ei--;
}
else
{
flag = false ;
break ;
}
}
if (flag)
Console.WriteLine( "Balanced" );
else
Console.WriteLine( "Not Balanced" );
}
public static void Main()
{
int n = 1235321;
BalancedNumber(n);
}
}
|
Javascript
function is_balanced_number(n) {
let s = n.toString();
let si = 0;
let ei = s.length - 1;
let flag = true ;
while (si < ei) {
if (s[si] == s[ei]) {
si++;
ei--;
}
else {
flag = false ;
break ;
}
}
if (flag) {
console.log( "Balanced" );
}
else {
console.log( "Not Balanced" );
}
}
let n = 1235321;
is_balanced_number(n);
|
Time Complexity: O(len), where len is the length of the string.
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...