Start learning free

LESSON 85 OF 414 · CONDITIONAL STATEMENTS - CHALLENGES

Absolute Difference in Scala: Early Return with if

Practice Read the full explanation

The absolute difference is a measure of the difference between two numbers but expressed as a positive number.

In other words, it’s taking the difference between two values (x - y) and then calculating the absolute value of the result.

Create a function that takes in two numbers a and b and returns their absolute difference.

Examples:

absDiff(3, 0) ➞ 3

absDiff(0, 3) ➞ 3

absDiff(10, -1) ➞ 11

absDiff(-5, -7) ➞ 2

absDiff(6, -6) ➞ 12

absDiff(7, 7) ➞ 0

Note:

Do not use the abs() function provided by Scala (it will defeat the purpose of the challenge)


Hints:


If a is less then b, the absolute difference equals b - a. What if a is greater than or equal to b?

If a < b, we should return b - a. Otherwise, we should return a - b. We can use an if statement to put this in code.
STEP 1 OF 3
    Ctrl + Enter to check

    SCALA · CONDITIONAL STATEMENTS - CHALLENGES

    Recreating Absolute Difference in Scala, explained

    Problem overview

    The absolute difference of two numbers is the gap between them, written as a non-negative number. It does not matter which number comes first: absDiff(3, 0) and absDiff(0, 3) are both 3. The method absDiff receives two integers, a and b, and returns that gap without using the library's abs function.

    More examples: absDiff(10, -1) is 11, absDiff(-5, -7) is 2, and absDiff(6, -6) is 12. The statement also lists absDiff(1.5, 1.5), but the method in the editor takes Int values, so the code works on whole numbers.

    Approach: subtract the smaller from the larger

    Computing a - b is negative whenever a is smaller than b. The fix is to subtract the smaller number from the larger one, so the result is never negative:

    • if a < b, the answer is b - a;
    • otherwise (a is greater than or equal to b), the answer is a - b.

    When a equals b, either subtraction gives 0, so that case needs no special treatment.

    Two examples traced by hand. For absDiff(0, 3), the test 0 < 3 is true, so the method returns 3 - 0, which is 3. For absDiff(-5, -7), the test -5 < -7 is false, because -5 sits to the right of -7 on the number line. The method skips the if and returns -5 - (-7), which is 2.

    Another way to see the same thing is the definition itself: the absolute difference is the absolute value of a - b. Negating a negative value fixes its sign, as when recreating abs. Here the comparison tells you in advance which order of subtraction gives a non-negative result, so you never need to negate.

    Step by step

    1. Compare the numbers. Write an if statement with its closing brace whose condition is a < b.
    2. Return b - a inside it. With a the smaller number, subtracting it from b gives a result that is not negative.
    3. Return a - b below the if. No else is needed. When a < b is true, the method has already returned. So the last line only runs when a is greater than or equal to b, and then a - b is not negative.

    The solution in Scala

    class Solution {
        def absDiff(a: Int, b: Int): Int = {
            if (a < b) {
                return b - a
            }
            return a - b
        }
    }
    

    The Scala details worth knowing:

    • Early return. return leaves the method immediately, so code after the if only runs when the condition was false. This replaces the else and keeps the code flat.
    • Result type. A method that uses return needs its result type written out, here : Int.
    • The expression style. Because if/else is an expression and the last expression is the result, the same method is often written as if (a < b) b - a else a - b with no return at all.

    Edge cases and common mistakes

    • Equal numbers. absDiff(4, 4) skips the if and returns 4 - 4, which is 0.
    • Negative inputs. Subtracting a negative number adds: absDiff(10, -1) computes 10 - (-1), which is 11. absDiff(-5, -7) computes -5 - (-7), which is 2.
    • Subtracting in the wrong order. Returning a - b for every input gives -3 for absDiff(0, 3).
    • Overflow. Int wraps silently at 32 bits. For a = Int.MaxValue and b = -1, the true gap is 2147483648, which does not fit, and the method returns -2147483648. Use Long if inputs can be that far apart.

    Testing

    • absDiff(3, 0) gives 3.
    • absDiff(0, 3) gives 3.
    • absDiff(10, -1) gives 11.
    • absDiff(-5, -7) gives 2.
    • absDiff(6, -6) gives 12.
    Keep going with ScalaAbout 340 step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free