FastPrepFastPrep
Problem Brief

Find the Wrong Digit Index in a Sum

FULLTIMEOA

You are given three non-negative integers: num1, num2, and givenSum.

Compare the correct result of num1 + num2 with givenSum digit by digit in base 10, starting from the least significant digit.

The ones place is index 0, the tens place is index 1, and so on. If one number has fewer digits than the other, treat the missing higher digits as 0 when comparing.

Return the index of the first mismatching digit. Return -1 if every digit matches.

Function Description

Complete the function findWrongDigitIndex in the editor below.

findWrongDigitIndex has the following parameters:

  1. long num1: the first addend
  2. long num2: the second addend
  3. long givenSum: the sum to compare against

Returns

int: the first mismatching digit index, or -1 if the sum is correct.

1Example 1

Input
num1 = 13, num2 = 7, givenSum = 19
Output
0
Explanation

The correct sum is 20. The ones digit differs immediately: 0 versus 9.

2Example 2

Input
num1 = 95, num2 = 5, givenSum = 100
Output
-1
Explanation

The provided sum is correct, so there is no mismatching digit.

Constraints

Limits and guarantees your solution can rely on.

  • 0 <= num1, num2, givenSum <= 10^18
  • Use base-10 digit comparison from least significant to most significant position.
public int findWrongDigitIndex(long num1, long num2, long givenSum) {
    // write your code here
}
Input

num1

13

num2

7

givenSum

19

Output

0

Sign in to submit your solution.