Skip to content
Programmingoneonone
Programmingoneonone
  • CS Subjects
    • Internet of Things (IoT)
    • Digital Communication
    • Human Values
    • Cybersecurity
  • Programming Tutorials
    • C Programming
    • Data structures and Algorithms
    • 100+ Java Programs
    • 100+ C Programs
  • HackerRank Solutions
    • HackerRank Algorithms Solutions
    • HackerRank C problems solutions
    • HackerRank C++ problems solutions
    • HackerRank Java problems solutions
    • HackerRank Python problems solutions
  • Work with US
Programmingoneonone
Programmingoneonone

HackerEarth Addition Errors problem solution

YASH PAL, 31 July 202411 February 2026
In this HackerEarth Addition Errors problem solution, Bob is a noob mathematician and he is not very comfortable with the addition of numbers. He is unaware of the concept of carrying that the addition of 2 numbers possesses. He every time forgets to take a carry while performing addition.
For example: suppose he adds 5 and 8 so he writes only 3 instead of 13. Similarly while adding 18 and 223 he writes it as 231.
So basically he ignores the carry every time. Your task is to compute the error in his addition algorithm. Error is defined as the absolute difference between Bob’s answer and the actual answer which was expected.
HackerEarth Addition Errors problem solution

HackerEarth Addition Errors problem solution.

#include<bits/stdc++.h>
using namespace std;
#define ll long long int
#include <sstream>

template <class T>
inline std::string to_string (const T& t)
{
std::stringstream ss;
ss << t;
return ss.str();
}
long long int to_int(string t)
{
stringstream g(t);
ll x = 0;
g >> x;
return x;
}
ll t,a,b;
ll get_rev(ll n)
{
ll num=0;
while(n>0)
{
ll dig=n%10;
num=num*10+dig;
n/=10;
}
return num;
}
void solve()
{
ll i;
cin>>t;
while(t--)
{
cin>>a>>b;
ll ans=a+b;
string sa=to_string(a);
string sb=to_string(b);
if(sa.size()>sb.size())
swap(sa,sb);
reverse(sa.begin(),sa.end());
reverse(sb.begin(),sb.end());
ll num=0;
string ns="";
for(i=0;i<sa.size();i++)
{
ll cc=(sa[i]-'0'+sb[i]-'0')%10;
string sk=to_string(cc);
ns=ns+sk;
}
for(i=sa.size();i<sb.size();i++)
{
ll cc=(sb[i]-'0')%10;
string sk=to_string(cc);
ns=ns+sk;
}
reverse(ns.begin(),ns.end());
num=to_int(ns);
cout<<ans-num<<"n";
}
}
int main()
{
solve();
return 0;
}

Second solution

import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.io.OutputStream;
import java.io.IOException;
import java.util.InputMismatchException;
import java.io.InputStreamReader;
import java.io.Writer;
import java.io.BufferedReader;
import java.io.InputStream;

public class Main {
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
FastScanner in = new FastScanner(inputStream);
FastPrinter out = new FastPrinter(outputStream);
AdditionErrors solver = new AdditionErrors();
int testCount = Integer.parseInt(in.next());
for (int i = 1; i <= testCount; i++)
solver.solve(i, in, out);
out.close();
}

static class AdditionErrors {
static long getBadSum(long a, long b) {
if (a == 0 && b == 0) return 0;
return getBadSum(a / 10, b / 10) * 10 + (a + b) % 10;
}

public void solve(int testNumber, FastScanner in, FastPrinter out) {
long a = in.nextLong();
long b = in.nextLong();
if (a < 1 || b < 1 || a > 1000000000000000000L || b > 1000000000000000000L) throw new AssertionError();
long sum = a + b;
long badSum = getBadSum(a, b);
out.println(sum - badSum);
}

}

static class FastScanner extends BufferedReader {
public FastScanner(InputStream is) {
super(new InputStreamReader(is));
}

public int read() {
try {
int ret = super.read();
return ret;
} catch (IOException e) {
throw new InputMismatchException();
}
}

public String next() {
StringBuilder sb = new StringBuilder();
int c = read();
while (isWhiteSpace(c)) {
c = read();
}
if (c < 0) {
return null;
}
while (c >= 0 && !isWhiteSpace(c)) {
sb.appendCodePoint(c);
c = read();
}
return sb.toString();
}

static boolean isWhiteSpace(int c) {
return c >= 0 && c <= 32;
}

public long nextLong() {
int c = read();
while (isWhiteSpace(c)) {
c = read();
}
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
long ret = 0;
while (c >= 0 && !isWhiteSpace(c)) {
if (c < '0' || c > '9') {
throw new NumberFormatException("digit expected " + (char) c + " found");
}
ret = ret * 10 + c - '0';
c = read();
}
return ret * sgn;
}

public String readLine() {
try {
return super.readLine();
} catch (IOException e) {
return null;
}
}

}

static class FastPrinter extends PrintWriter {
public FastPrinter(OutputStream out) {
super(out);
}

public FastPrinter(Writer out) {
super(out);
}

}
}
coding problems solutions HackerEarth HackerEarth

Post navigation

Previous post
Next post

Leave a Reply

Your email address will not be published. Required fields are marked *

Pages

  • About US
  • Contact US
  • Privacy Policy

Follow US

  • YouTube
  • LinkedIn
  • Facebook
  • Pinterest
  • Instagram
©2026 Programmingoneonone | WordPress Theme by SuperbThemes