Remove trailing zeros from the sum of two numbers ( Using Stack )
Last Updated :
06 Mar, 2022
Given two numbers A and B, the task is to remove the trailing zeros present in the sum of the two given numbers using a stack.
Examples:
Input: A = 124, B = 186
Output: 31
Explanation: Sum of A and B is 310. Removing the trailing zeros modifies the sum to 31.
Input: A=130246, B= 450164
Output : 58041
Approach: The given problem can be solved using the string and stack data structures. Follow the steps below to solve the problem:
- Calculate A + B and store it in a variable, say N.
- Initialize a stack < char >, say S, to store the digits of N.
- Convert the integer N to string and then push the characters into the stack S.
- Iterate while S is not empty(), If the top element of the stack is ‘0’, then pop it out of the stack. Otherwise, break.
- Initialize a string, say res, to store the resultant string.
- Iterate while S is not empty(), and push all the characters in res and, then pop the top element.
- Reverse the string res and print the res as the answer.
Below is the implementation of the above approach:
C++14
#include <bits/stdc++.h>
using namespace std;
string removeTrailing( int A, int B)
{
int N = A + B;
stack< int > s;
string strsum = to_string(N);
for ( int i = 0; i < strsum.length(); i++) {
s.push(strsum[i]);
}
while (s.top() == '0' )
s.pop();
string res = "" ;
while (!s.empty()) {
res = res + char (s.top());
s.pop();
}
reverse(res.begin(), res.end());
return res;
}
int main()
{
int A = 130246, B = 450164;
cout << removeTrailing(A, B);
return 0;
}
|
Java
import java.io.*;
import java.util.*;
class GFG{
static String removeTrailing( int A, int B)
{
int N = A + B;
Stack<Character> s = new Stack<Character>();
String strsum = Integer.toString(N);
for ( int i = 0 ; i < strsum.length(); i++)
{
s.push(strsum.charAt(i));
}
while (s.peek() == '0' )
{
s.pop();
}
String res = "" ;
while (s.empty() == false )
{
res = res + ( char )s.peek();
s.pop();
}
StringBuilder str = new StringBuilder();
str.append(res);
str.reverse();
return str.toString();
}
public static void main (String[] args)
{
int A = 130246 , B = 450164 ;
System.out.println(removeTrailing(A, B));
}
}
|
Python3
def removeTrailing(A, B):
N = A + B
s = []
strsum = str (N)
for i in range ( len (strsum)):
s.append(strsum[i])
while (s[ - 1 ] = = '0' ):
s.pop()
res = ""
while ( len (s) ! = 0 ):
res = res + (s[ - 1 ])
s.pop()
res = list (res)
res.reverse()
res = ''.join(res)
return res
if __name__ = = "__main__" :
A = 130246
B = 450164
print (removeTrailing(A, B))
|
C#
using System;
using System.Collections.Generic;
class GFG{
static string removeTrailing( int A, int B)
{
int N = A + B;
Stack< char > s = new Stack< char >();
string strsum = N.ToString();
for ( int i = 0; i < strsum.Length; i++)
{
s.Push(strsum[i]);
}
while (s.Peek() == '0' )
{
s.Pop();
}
string res = "" ;
while (s.Count != 0)
{
res = res + ( char )s.Peek();
s.Pop();
}
char [] str = res.ToCharArray();
Array.Reverse(str);
return new string ( str);
}
static public void Main()
{
int A = 130246, B = 450164;
Console.WriteLine(removeTrailing(A, B));
}
}
|
Javascript
<script>
function removeTrailing(A, B) {
let N = A + B;
let s = new Array();
let strsum = N.toString();
for (let i = 0; i < strsum.length; i++) {
s.push(strsum.charAt(i));
}
while (s[s.length-1] === '0' ) {
s.pop();
}
let res = "" ;
while (s.length != 0) {
res = res.concat(s[s.length-1]);
s.pop();
}
let str = "" ;
str = str.concat(res)
str = str.split( "" ).reverse().join( "" );
return str.toString();
}
let A = 130246, B = 450164;
document.write(removeTrailing(A, B));
</script>
|
Time Complexity: O(len(A + B))
Auxiliary Space: O(len(A + B))
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...