Find the Wrong Digit Index in a Sum
You are given three non-negative integers: num1, num2, and givenSum.
Compute the correct result of num1 + num2, then compare it with givenSum digit by digit in base 10, starting from the least significant digit (index 0).
The ones place is index 0, the tens place is index 1, and so on. If the correct sum and givenSum have different numbers of digits, treat the missing higher-order digits of the shorter number as 0 when comparing. The comparison continues through the maximum number of digit positions across both numbers.
Return the index of the first mismatching digit. Return -1 if every digit matches (i.e., givenSum equals num1 + num2).
Complete the function findWrongDigitIndex in the editor below.
findWrongDigitIndex has the following parameters:
long num1: the first addendlong num2: the second addendlong givenSum: the sum to compare against
Returns
int: the index of the first mismatching digit (starting from 0 at the ones place), or -1 if all digits match.
num1 = 13 num2 = 7 givenSum = 19 return = 0
The correct sum is 20. The ones digit differs immediately: 0 versus 9.
num1 = 95 num2 = 5 givenSum = 100 return = -1
The provided sum is correct, so there is no mismatching digit.
Constraints:
0 <= num1, num2, givenSum <= 1018- The correct sum
num1 + num2may exceed1018; use arbitrary-precision arithmetic when computing the correct sum. - Digits are compared in base 10 from least significant (index 0) to most significant position.
- Missing higher-order digits are treated as
0when the correct sum andgivenSumhave different numbers of digits. - Return
-1if and only ifgivenSum == num1 + num2.
public int findWrongDigitIndex(long num1, long num2, long givenSum) {
// write your code here
}