Saturday, January 11, 2025
Google search engine
HomeLanguagesCheck if a + b = c is valid after removing all...

Check if a + b = c is valid after removing all zeroes from a, b and c

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++




// C++ implementation of the approach
#include<bits/stdc++.h>
#include<string>
#include<iostream>
#include<sstream>
 
using namespace std;
 
// Function to remove zeroes from a number
int remove(int x)
{
    // Converting x into a string
     
    string y = to_string(x);
     
    // To store the new integer without 0s
    string num;
    int i;
    for(i = 0; i < y.length(); i++)
    {
        // Skip if current character is 0
        if(y[i] == 0)
            continue;
        num += y[i];
    }
         
    // Return the integer after removing 0s
    return stoi(num);
}
 
// Function that returns true if
// the given condition is satisfied
bool check(int a, int b)
{
    // Calculate c
    int c = a + b;
     
    // Remove 0s from a, b and c
    a = remove(a);
    b = remove(b);
    c = remove(c);
     
    // Check if the equation is still correct
    if((a + b) == c)
        return true;
    else
        return false;
}
 
// Driver code
int main()
{
    int a = 101;
    int b = 102;
     
    if(check(a, b))
        cout << "Yes";
    else
        cout << "No";
}
 
// This code is contributed by ita_c


Java




// Java implementation of the approach
import java.util.*;
 
class GFG{
 
// Function to remove zeroes from a number    
public static int remove(int x)
{
     
    // Converting x into a string
    String y = String.valueOf(x);
     
    // To store the new integer without 0s
    String num = "";
    int i;
     
    for(i = 0; i < y.length(); i++)
    {
        
       // Skip if current character is 0
       if(y.charAt(i) == 0)
          continue;
       num += y.charAt(i);
    }
         
    // Return the integer after
    // removing 0s
    return Integer.parseInt(num);
}
 
 
// Function that returns true if
// the given condition is satisfied
public static boolean check(int a, int b)
{
     
    // Calculate c
    int c = a + b;
     
    // Remove 0s from a, b and c
    a = remove(a);
    b = remove(b);
    c = remove(c);
     
    // Check if the equation is
    // still correct
    if((a + b) == c)
        return true;
    else
        return false;
}
 
// Driver code
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");
}
}
 
// This code is contributed by grandmaster


Python3




# Python3 implementation of the approach
 
# Function that returns true if
# the given condition is satisfied
def check(a, b):
     
    # Calculate c
    c = a + b
     
    # Remove 0s from a, b and c
    a = remove(a)
    b = remove(b)
    c = remove(c)
     
    # Check if the equation is still correct
    if((a + b) == c):
        return True
    else:
        return False
 
# Function to remove zeroes from a number
def remove(x):
     
    # Converting x into a string
    y = str(x)
     
    # To store the new integer without 0s
    num = ""
    for i in range(len(y)):
         
        # Skip if current character is 0
        if(y[i] == "0"):
            continue
        num += y[i]
         
    # Return the integer after removing 0s
    return int(num)
 
# Driver code
a = 101
b = 102
 
if(check(a, b)):
    print("Yes")
else:
    print("No")


C#




// C# implementation of the approach
using System;
  
class GFG{
  
// Function to remove zeroes from a number    
public static int remove(int x)
{
     
    // Converting x into a string
    string y = x.ToString();
      
    // To store the new integer without 0s
    string num = "";
    int i;
      
    for(i = 0; i < y.Length; i++)
    {
         
        // Skip if current character is 0
        if (y[i] == 0)
            continue;
             
        num += y[i];
    }
          
    // Return the integer after
    // removing 0s
    return Int32.Parse(num);
}
  
// Function that returns true if
// the given condition is satisfied
public static bool check(int a, int b)
{
     
    // Calculate c
    int c = a + b;
      
    // Remove 0s from a, b and c
    a = remove(a);
    b = remove(b);
    c = remove(c);
      
    // Check if the equation is
    // still correct
    if ((a + b) == c)
        return true;
    else
        return false;
}
  
// Driver code
public static void Main(string[] args)
{
    int a = 101;
    int b = 102;
      
    if (check(a, b))
        Console.Write("Yes");
    else
        Console.Write("No");
}
}
 
// This code is contributed by rutvik_56


PHP




<?php
// PHP implementation of the approach
 
// Function that returns true if
// the given condition is satisfied
function check($a, $b)
{
     
    // Calculate c
    $c = $a + $b ;
     
    // Remove 0s from a, b and c
    $a = remove($a);
    $b = remove($b);
    $c = remove($c);
     
    // Check if the equation is
    // still correct
    if(($a + $b) == $c)
        return true;
    else
        return false;
}
 
// Function to remove zeroes
// from a number
function remove($x)
{
     
    // Converting x into a string
    $y = (string)$x;
     
    // To store the new integer without 0s
    $num = "";
    for ($i = 0; $i < strlen($y); $i++)
    {
         
        // Skip if current character is 0
        if($y[$i] == "0")
            continue ;
        $num .= $y[$i];
    }
         
    // Return the integer after removing 0s
    return (int)$num;
}
 
// Driver code
$a = 101;
$b = 102;
 
if(check($a, $b))
    echo "Yes";
else
    echo "No";
 
// This code is contributed by Ryuga
?>


Javascript




<script>
 
// JavaScript implementation of the approach
 
// Function to remove zeroes from a number
function remove(x)
{
    // Converting x into a string
     
    var y = x.toString();
     
    // To store the new integer without 0s
    var num = "";
    var i;
    for(i = 0; i < y.length; i++)
    {
        // Skip if current character is 0
        if(y[i] == 0)
            continue;
        num += y[i];
    }
         
    // Return the integer after removing 0s
    return parseInt(num);
}
 
// Function that returns true if
// the given condition is satisfied
function check(a, b)
{
    // Calculate c
    var c = a + b;
     
    // Remove 0s from a, b and c
    a = remove(a);
    b = remove(b);
    c = remove(c);
     
    // Check if the equation is still correct
    if((a + b) == c)
        return true;
    else
        return false;
}
 
// Driver code
var a = 101;
var b = 102;
 
if(check(a, b))
    document.write( "Yes");
else
    document.write( "No");
 
</script>


Output

Yes

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);
 
    // Remove all occurrences of '0' in a
    str_a.erase(remove(str_a.begin(), str_a.end(), '0'), str_a.end());
 
    // Remove all occurrences of '0' in b
    str_b.erase(remove(str_b.begin(), str_b.end(), '0'), str_b.end());
 
    // Remove all occurrences of '0' in c
    str_c.erase(remove(str_c.begin(), str_c.end(), '0'), str_c.end());
 
    // Check if the sum of a and b is equal to c
    if (stoi(str_a) + stoi(str_b) == stoi(str_c)) {
        cout << "Yes" << endl;
    } else {
        cout << "No" << endl;
    }
 
    return 0;
}


Java




// Java code for the above approach
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");
        }
    }
}
 
// This code is contributed by lokeshpotta20.


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")
#This code is contributed by Edula Vinay Kumar Reddy


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");


Output

Yes

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)

Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
You’ll access excellent video content by our CEO, Sandeep Jain, tackle common interview questions, and engage in real-time coding contests covering various DSA topics. We’re here to prepare you thoroughly for online assessments and interviews.
Ready to dive in? Explore our free demo content and join our DSA course, trusted by over 100,000neveropen! Whether it’s DSA in C++, Java, Python, or JavaScript we’ve got you covered. Let’s embark on this exciting journey together!

Dominic Rubhabha-Wardslaus
Dominic Rubhabha-Wardslaushttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Recent Comments