Check if a + b = c is valid after removing all zeroes from a, b and c
Last Updated :
15 Mar, 2023
Given two integers a and b. Now, c can be found as a + b = c. The task is to check if the equation is still valid after removing all zeroes from a, b and c. If valid then print Yes else print No.
Examples:
Input: a = 101, b = 102
Output: Yes
Current equation is 101 + 102 = 203
After removing 0s, 11 + 12 = 23 (which is still correct)
Input: a = 105, b = 106
Output: No
105 + 106 = 211
15 + 16 = 211 (Incorrect)
Approach:
- Calculate c.
- Remove all 0s from a, b and c.
- Check if the new values form a correct equation.
Below is the implementation of the above approach:
C++
#include<bits/stdc++.h>
#include<string>
#include<iostream>
#include<sstream>
using namespace std;
int remove ( int x)
{
string y = to_string(x);
string num;
int i;
for (i = 0; i < y.length(); i++)
{
if (y[i] == 0)
continue ;
num += y[i];
}
return stoi(num);
}
bool check( int a, int b)
{
int c = a + b;
a = remove (a);
b = remove (b);
c = remove (c);
if ((a + b) == c)
return true ;
else
return false ;
}
int main()
{
int a = 101;
int b = 102;
if (check(a, b))
cout << "Yes" ;
else
cout << "No" ;
}
|
Java
import java.util.*;
class GFG{
public static int remove( int x)
{
String y = String.valueOf(x);
String num = "" ;
int i;
for (i = 0 ; i < y.length(); i++)
{
if (y.charAt(i) == 0 )
continue ;
num += y.charAt(i);
}
return Integer.parseInt(num);
}
public static boolean check( int a, int b)
{
int c = a + b;
a = remove(a);
b = remove(b);
c = remove(c);
if ((a + b) == c)
return true ;
else
return false ;
}
public static void main(String[] args)
{
int a = 101 ;
int b = 102 ;
if (check(a, b))
System.out.println( "Yes" );
else
System.out.println( "No" );
}
}
|
Python3
def check(a, b):
c = a + b
a = remove(a)
b = remove(b)
c = remove(c)
if ((a + b) = = c):
return True
else :
return False
def remove(x):
y = str (x)
num = ""
for i in range ( len (y)):
if (y[i] = = "0" ):
continue
num + = y[i]
return int (num)
a = 101
b = 102
if (check(a, b)):
print ( "Yes" )
else :
print ( "No" )
|
C#
using System;
class GFG{
public static int remove( int x)
{
string y = x.ToString();
string num = "" ;
int i;
for (i = 0; i < y.Length; i++)
{
if (y[i] == 0)
continue ;
num += y[i];
}
return Int32.Parse(num);
}
public static bool check( int a, int b)
{
int c = a + b;
a = remove(a);
b = remove(b);
c = remove(c);
if ((a + b) == c)
return true ;
else
return false ;
}
public static void Main( string [] args)
{
int a = 101;
int b = 102;
if (check(a, b))
Console.Write( "Yes" );
else
Console.Write( "No" );
}
}
|
PHP
<?php
function check( $a , $b )
{
$c = $a + $b ;
$a = remove( $a );
$b = remove( $b );
$c = remove( $c );
if (( $a + $b ) == $c )
return true;
else
return false;
}
function remove( $x )
{
$y = (string) $x ;
$num = "" ;
for ( $i = 0; $i < strlen ( $y ); $i ++)
{
if ( $y [ $i ] == "0" )
continue ;
$num .= $y [ $i ];
}
return (int) $num ;
}
$a = 101;
$b = 102;
if (check( $a , $b ))
echo "Yes" ;
else
echo "No" ;
?>
|
Javascript
<script>
function remove(x)
{
var y = x.toString();
var num = "" ;
var i;
for (i = 0; i < y.length; i++)
{
if (y[i] == 0)
continue ;
num += y[i];
}
return parseInt(num);
}
function check(a, b)
{
var c = a + b;
a = remove(a);
b = remove(b);
c = remove(c);
if ((a + b) == c)
return true ;
else
return false ;
}
var a = 101;
var b = 102;
if (check(a, b))
document.write( "Yes" );
else
document.write( "No" );
</script>
|
Time Complexity: O(1)
Auxiliary Space: O(1)
One approach is to use the str.replace() method to remove all zeroes from a, b and c, and then check if the resulting values form a correct equation.
Here is an example of how this can be done:
C++
#include <iostream>
#include <algorithm>
#include <string>
using namespace std;
int main() {
int a = 101;
int b = 102;
int c = a + b;
string str_a = to_string(a);
string str_b = to_string(b);
string str_c = to_string(c);
str_a.erase( remove (str_a.begin(), str_a.end(), '0' ), str_a.end());
str_b.erase( remove (str_b.begin(), str_b.end(), '0' ), str_b.end());
str_c.erase( remove (str_c.begin(), str_c.end(), '0' ), str_c.end());
if (stoi(str_a) + stoi(str_b) == stoi(str_c)) {
cout << "Yes" << endl;
} else {
cout << "No" << endl;
}
return 0;
}
|
Java
import java.io.*;
class GFG {
public static void main(String[] args)
{
int a = 101 ;
int b = 102 ;
int c = a + b;
String aString
= Integer.toString(a).replace( "0" , "" );
String bString
= Integer.toString(b).replace( "0" , "" );
String cString
= Integer.toString(c).replace( "0" , "" );
if (Integer.parseInt(aString)
+ Integer.parseInt(bString)
== Integer.parseInt(cString)) {
System.out.println( "Yes" );
}
else {
System.out.println( "No" );
}
}
}
|
Python3
a = 101
b = 102
c = a + b
a = str (a).replace( "0" , "")
b = str (b).replace( "0" , "")
c = str (c).replace( "0" , "")
if int (a) + int (b) = = int (c):
print ( "Yes" )
else :
print ( "No" )
|
C#
using System;
namespace ConsoleApp1
{
class GFG
{
static void Main( string [] args)
{
int a = 101;
int b = 102;
int c = a + b;
a = int .Parse(a.ToString().Replace( "0" , "" ));
b = int .Parse(b.ToString().Replace( "0" , "" ));
c = int .Parse(c.ToString().Replace( "0" , "" ));
if (a + b == c)
{
Console.WriteLine( "Yes" );
}
else
{
Console.WriteLine( "No" );
}
}
}
}
|
Javascript
let a = "101" ;
let b = "102" ;
let x = parseInt(a) + parseInt(b);
let c=x.toString();
a = a.replace( "0" , "" );
b = b.replace( "0" , "" );
c = c.replace( "0" , "" );
if (parseInt(a) + parseInt(b) == parseInt(c))
console.log( "Yes" );
else
console.log( "No" );
|
Time Complexity: O(1) (Given is integer, even largest integer contains constant digits which is constant time to replace “0”s )
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...