You are on page 1of 5

import java.util.

*;

class BigIntegerAddition {
        public static void main(String args[]) {
                Scanner sc = new Scanner(System.in);
                String num1 = sc.next();
                String num2 = sc.next();

                // System.out.println(digitAt("5467", 4));
                // System.out.println(digitAt("5467", 5));

                String ans = bigIntegerAddition(num1, num2);


                // System.out.println(digitAt("5467", 0));
                // System.out.println(digitAt("5467", 1));

                System.out.println(ans);

    }

        // 5467, 0 => 7
        // 5467, 1 => 6
        // 5467, 5 => 0
        static int digitAt(String num, int positionStartingFromLsb) {
                if (positionStartingFromLsb > num.length()-1) {
                        return 0;
        }

                return num.charAt(num.length()-positionStartingFromLsb-1) - '0';


    }

        // 1. String is immutable and passed by value. Which means this function cannot accept
String, but should rather use StringBuilder.
        // 2. reversedSum
        static void buildDigits(StringBuilder reversedNumber, int digit) {
                reversedNumber.append(digit);
    }
        static String bigIntegerAddition(String num1, String num2) {
                // TODO: Swap the numbers so that num1 > num2
                int carry = 0;
                StringBuilder reversedSum = new StringBuilder();

                for (int i = 0; i < num1.length() || i < num2.length(); i++) {


                        int sumOfDigits = digitAt(num1, i) + digitAt(num2, i) + carry;
                        // 814
                        // 213
                              // 7
                              // 27
                              // 027
                        buildDigits(reversedSum, sumOfDigits%10);
                        carry = sumOfDigits/10;
        }

                // If carry is not 0, then prefix the sum with 1.


                if (carry != 0) {
                        buildDigits(reversedSum, carry);
        }

                return reversedSum.reverse().toString();
    }
}

// Milestone 1: Understand the problem clearly


// - Ask questions & clarify the problem statement clearly.
// - Take an example or two to confirm your understanding of the input/output & extend it to
testcases
// Eg: 1
// 10
// 34
// O/P: 44

// Eg: 2
// 0
// 0
// O/P: 0
// Eg: 3
// 0000
// 00
// O/P: 0

// eg: 4
// 009
// 9
// O/P: 18

// eg: 5
// 19
// 18
// 37

// eg: 6
// 99
// 99
// 198

// eg: 7
// -100
// -9
// -109

// // Milestone 2: Finalize approach & execution plan


// // - Understand what type of problem you are solving. -> Logic
// // - Brainstorm multiple ways to solve the problem and pick one
// #1: Go through each of the digits and keep adding each digit one by one, make sure that
you take care of carry in the next digit's addition.
// - Always traverse the string backwards while doing the addition.
// 1999
//    991
// - First take the least significant digit (9, 1)
// - Add them together as integers
// - Store the digit after doing modulo 10 - you want to store 0
// - Carry if any while adding the next digit - (9, 9) + carry from the previous addition
// - If there is a pending carry in the end after traversing all digits, make sure to add the carry
as prefix
// // - Get to a point where you can explain your approach to a 10 year old
// // - Take a stab at the high level logic & write it down.
// num1, num2
// 1999908
// 0009991

// Swap the numbers so that num1 > num2


// carry = 0
// String sum = len(num1) + 1;
// Loop through num2 in the reverse order - i
// {
//          // 9 + 1 = 10
//          sumOfDigits = num1.digitAt(i) + num2.digitAt(i) + carry;
//          sum[i] = sumOfDigits % 10;
//          carry = sumOfDigits/10;
// }

// // Some logic to take care of remaining digits in num1.

// // If carry is not 0, then prefix the sum with 1.

// // - Try to offload processing to functions & keeping your main code small.
// digitAt(num, i) => Returns the digit. If there is no digit that exists, it can return 0.

// Milestone 3: Code by expanding your pseudocode


// - Make sure you name the variables, functions clearly.
// - Avoid constants in your code unless necessary; go for generic functions, you can use
examples for your thinking though.
// - Use libraries as much as possible

// Milestone 4: Prove to the interviewer that your code works with unit tests
// - Make sure you check boundary conditions

// Time & storage complexity


// Suggest optimizations
  

You might also like