Start learning free

LESSON 83 OF 338 · CONDITIONAL STATEMENTS - CHALLENGES

Recreating Absolute Difference in Go: absDiff Without math.Abs

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(1.5, 1.5) ➞ 0

Note:

Do not use the abs() function provided by Go (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

    GO · CONDITIONAL STATEMENTS - CHALLENGES

    Recreating Absolute Difference in Go, explained

    Problem overview

    The function absDiff receives two integers, a and b, and returns the distance between them as a number that is never negative. This is the absolute value of a - b, computed without calling an absolute-value function.

    For a = 10 and b = -1, the difference a - b is 11. For a = -5 and b = -7, it is 2. In both cases the order of the arguments does not change the answer: absDiff(3, 0) and absDiff(0, 3) both return 3.

    Approach

    One way is to compute a - b and, if the result is negative, flip its sign. A shorter route is to decide which argument is larger and subtract the smaller from it. The result of "larger minus smaller" can never be negative, so no sign fix is needed. Only one comparison, a < b, tells you which of the two is smaller. That is why the lesson uses it, and why it needs just one if and no helper variables.

    Step by step

    1. Compare the two numbers. Write an if with the condition a < b, with braces and no parentheses.
    2. Handle the case where a is smaller. Inside the if, return b - a. Here b is the larger number, so the result is not negative.
    3. Handle everything else after the block. Below the if, return a - b. When the if was true, the function has already returned, so this line runs only when a is greater than or equal to b. Then a - b is not negative. When the two are equal, it is zero.

    The solution in Go

    func absDiff(a int, b int) int {
        if a < b {
            return b - a
        }
        return a - b
    }
    

    Notes on the Go details:

    • No else needed. A return ends the function immediately, so code after an if that returns runs only when the condition was false. This early-return shape avoids a nested else, and it is common in Go.
    • The final return. Go requires the function to end with a return statement, which the last line provides. Without it, the compiler reports missing return.
    • Parameter list. a int, b int can also be written a, b int. The lesson's form repeats the type for each parameter, and both are equivalent.
    • Integer types. The signature uses int, so decimal arguments such as 1.5 are not accepted. A version for decimals needs float64 parameters and a float64 result.

    Tracing two calls

    For absDiff(0, 3), the test 0 < 3 is true, so the function returns 3 - 0, which is 3. For absDiff(10, -1), the test 10 < -1 is false, so the if body is skipped and the function returns 10 - (-1), which is 11. Each call reaches exactly one return.

    Complexity

    The function does one comparison and one subtraction, so time is O(1) and extra space is O(1).

    Edge cases and common mistakes

    • Equal numbers. absDiff(6, 6) goes to the final return and gives 0. Using a <= b in the condition would give the same answer, since b - a is also zero.
    • Opposite signs. absDiff(6, -6) returns 12: subtracting a negative number adds its size. The branch is chosen by a < b alone, so no special case for signs is needed.
    • Both negative. absDiff(-5, -7) is 2. Here a is larger than b, so the function returns a - b, which is -5 - (-7).
    • Always subtracting in one order. Returning only a - b is negative for absDiff(0, 3), which gives -3.
    • Overflow. Integers wrap silently in Go. With extreme values such as absDiff(9223372036854775807, -1), the true difference does not fit in an int, and the function returns -9223372036854775808. Ordinary inputs never get near this.

    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 GoAbout 340 step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free