Multiply Large Numbers using Grid Method
Given two large numbers A and B, the task is to find the product of these two numbers using Grid Method.
Examples:
Input: A = 23, B = 15
Output: 345
Input: A = 321, B = 69
Output: 22149
Approach:
- Create 2D Array of N Rows and M columns where N is number of digit in first number and M is number of digit in second number.
- Multiply each element of row with each element of column
- Total Number of Diagonal = Row + Columns – 1
= 2 + 2 -1
= 3
- Create 1D Array which contains the addition of elements in each diagonal
d3 = 2
d2 = 13
d1 = 15
Diagonal sum[] = {2, 13, 15}
output = “”
total = 0
i = DiagonalSum.length – 1
- Repeat in reverse order of insertion except for first element in Diagonal Sum[] Array
total = total + DiagonalSum[i]. If total contain more than single digit then total = all digit from total except unit place digit. output = unit_place_digit + output else total = 0
- total = total + DiagonalSum[0]
output = total + output
Below is the implementation of the above approach:
Java
// Java program to multiply Large // numbers using the grid method class GFG { // Function to return the multiplication of a and b public static String multiply(String a, String b) { boolean flag1 = false ; boolean flag2 = false ; a = a.trim(); b = b.trim(); // To check whether numbers are // negative or positive if (a.charAt( 0 ) == '-' ) { a = a.replace("-", ""); flag1 = true ; } if (b.charAt( 0 ) == '-' ) { b = b.replace("-", ""); flag2 = true ; } // To store the result of // multiplication String out = ""; // To create matrix(Grid) of row * column int row = a.length(); int column = b.length(); int [][] c = new int [row][column]; for ( int i = 0 ; i < row; i++) { for ( int j = 0 ; j < column; j++) { int n1 = Character .getNumericValue( a.charAt(i)); int n2 = Character .getNumericValue( b.charAt(j)); c[i][j] = n1 * n2; } } // To create 1D array of (row+column-1) size // which is equal to total number // of diagonal in matrix int [] sum = new int [row + column - 1 ]; int m = 0 ; // To add elements of each diagonals for ( int i = 0 ; i < row; i++) { int k = i; int add = 0 ; for ( int j = 0 ; j < column && k >= 0 ; j++, k--) { add = add + c[k][j]; } sum[m] = add; m = m + 1 ; } for ( int k = 1 ; k < column; k++) { int i = row - 1 ; int j = k; int add = 0 ; while (j < column && i >= 0 ) { add = add + c[i][j]; j = j + 1 ; i = i - 1 ; } sum[m] = add; m = m + 1 ; } // To check both numbers are not // single digit number if (sum.length != 1 ) { String temp = Integer .toString( sum[sum.length - 1 ]); int t = 0 ; // Repeat element in "sum" Array // in reverse order for ( int n = sum.length - 1 ; n >= 1 ; n--) { // Add element with result "t" t = t + sum[n]; // Convert integer element into String // which is sum of all elements // of particular diagonal temp = Integer.toString(t); if (temp.length() > 1 ) { // If the number contains more than a single-digit // then copy all the digit into "temp" // as String except for the unit place digit String str = temp.substring( 0 , temp.length() - 1 ); t = Integer.parseInt(str); } else { t = 0 ; } // Concat unit place digit at the // beginning of String "out" out = temp.charAt(temp.length() - 1 ) + out; } // Add first element with result "t" t = t + sum[ 0 ]; temp = Integer.toString(t); out = temp + out; } else { out = out + sum[ 0 ]; } StringBuffer s = new StringBuffer(out); // To remove Zero's from the beginning // of the multiplication result for ( int i = 0 ; i < s.length() - 1 ; i++) { if (s.charAt(i) == '0' ) { s.deleteCharAt(i); i = i - 1 ; } else { break ; } } out = s.toString(); // Check if the result of multiplication // operation is zero if (!out.equals(" 0 ")) { // If one of two numbers is negative then // assign minus sign to the result of // multiplication operation if (flag1 == true && flag2 == false ) { out = "-" + out; } else if (flag2 == true && flag1 == false ) { out = "-" + out; } } return out; } // Driver code public static void main(String args[]) { String str1 = " 123456789 "; String str2 = " 987654321 "; System.out.println(multiply(str1, str2)); str1 = " 1235421415454545454545454544 "; str2 = " 1714546546546545454544548544544545 "; System.out.println(multiply(str1, str2)); } } |
Output:
121932631112635269
2118187521397235888154583183918321221520083884298838480662480
121932631112635269
2118187521397235888154583183918321221520083884298838480662480
Time Complexity: O(row * column)
Auxiliary Space: O(row * column)