Divide large number represented as string in JavaIn the input, a large number (in the form of a string) is given to us. We need to divide it by another number (in the form of an int data type). Our task is to find the division of these numbers and return the result of those numbers. Example 1: Input: int num = 124587785421 int div = 963 Output: The result is 129374647 Example 2: Input: int num = 12548796324587 int div = 120 Output: The result is 104573302704 Example 3: Input: int num = 475213698410236589560 int div = 33 Output: The result is 14400415103340502713 Approach: Naïve ApproachAlgorithm:Step 1: Create a StringBuilder in order to save the result. Step 2: Create a character array from the input string. Step 3: Set the carry initial value to 0. Step 4: Carry * 10 + current_digit is the formula to find the current number for each character in the array. Step 5: As current_number / divisor, compute the partial quotient and add it to the result. Step 6: Implementing the current_number% divisor, update carry. Step 7: Find the first non-zero character by iterating over the result. Step 8: From this character, return the substring to the end. Step 9: Return "0" in the event that every character in the result is zero. Step 10: Finally, print the quotient. Implementation:FileName: StringLongDivision.java Output: The result is: 104573302704 Complexity Analysis: The above code's time complexity is O(N), and its space complexity is O(N), where N represents the length of the string Number. |
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India