Start learning free

LESSON 82 OF 410 · CONDITIONAL STATEMENTS - CHALLENGES

Recreating the abs() function in Java with O(1) Time Complexity

Practice Read the full explanation

The absolute value function (abs() in many languages) returns the absolute value of a number. This means that it returns a number's positive value. You can think of it as the distance away from zero.

Create your own function that recreates this functionality, using the name and parameter n from the starter code.

Examples:

abs(-12) ➞ 12

abs(-544) ➞ 544

abs(250) ➞ 250

abs(0) ➞ 0

abs(-14) ➞ 14

Note:

Do not use the absolute value function provided by Java (it will defeat the purpose of the challenge)


Hints:


You can make use of < operator with an if statement to check if n is negative.

You can add an else statement to handle the case when n is positive.
STEP 1 OF 3
    Ctrl + Enter to check

    JAVA · CONDITIONAL STATEMENTS - CHALLENGES

    Recreating Abs in Java, explained

    Understanding the Problem

    The abs() function returns the absolute value of a number, which is the number's positive value. Essentially, it measures the distance of a number from zero on the number line.

    We need to create our own function abs() that mimics this functionality without using Java's built-in abs() function.

    Examples:

    abs(-1.217197940) ➞ 1.21719794
    
    abs(-544.0) ➞ 544
    
    abs(250.3) ➞ 250.3
    
    abs(0) ➞ 0
    
    abs(-0.14) ➞ 0.14
    

    Approach

    To solve this problem, we need to understand the core challenge: determining whether a number is negative and converting it to its positive counterpart if it is.

    We can achieve this by using a simple conditional check:

    • If the number is negative, return its positive counterpart by multiplying it by -1.
    • If the number is positive or zero, return it as is.

    Naive Solution

    A naive solution involves checking if the number is less than zero and then converting it to positive if it is. This approach is straightforward and efficient with a time complexity of O(1).

    Optimized Solution

    The naive solution is already optimal for this problem since it operates in constant time O(1). There are no further optimizations needed.

    Algorithm

    1. Check if the number is less than zero.
    2. If it is, multiply the number by -1 to make it positive.
    3. If it is not, return the number as is.

    Code Implementation

    
    public class AbsoluteValue {
        // Function to return the absolute value of a number
        public static double abs(double n) {
            // Check if the number is negative
            if (n < 0) {
                // If negative, return the positive counterpart
                return -n;
            } else {
                // If positive or zero, return the number as is
                return n;
            }
        }
    
        // Main method to test the abs function
        public static void main(String[] args) {
            System.out.println(abs(-1.217197940)); // ➞ 1.21719794
            System.out.println(abs(-544.0));       // ➞ 544
            System.out.println(abs(250.3));        // ➞ 250.3
            System.out.println(abs(0));            // ➞ 0
            System.out.println(abs(-0.14));        // ➞ 0.14
        }
    }
    

    Complexity Analysis

    The time complexity of this solution is O(1) because it involves a single conditional check and a possible multiplication, both of which are constant-time operations.

    The space complexity is also O(1) as no additional space is required beyond a few variables.

    Edge Cases

    Consider the following edge cases:

    • Zero: abs(0) ➞ 0
    • Negative numbers: abs(-0.14) ➞ 0.14
    • Large negative numbers: abs(-1000000) ➞ 1000000

    Our algorithm handles these cases effectively by design.

    Testing

    To test the solution comprehensively, consider a variety of test cases:

    • Simple positive and negative numbers
    • Zero
    • Floating-point numbers
    • Large magnitude numbers

    Use a testing framework like JUnit for automated testing.

    Thinking and Problem-Solving Tips

    When approaching such problems:

    • Break down the problem into smaller, manageable parts.
    • Think about edge cases and how your solution handles them.
    • Practice similar problems to improve your problem-solving skills.

    Conclusion

    Recreating the abs() function is a simple yet fundamental exercise in understanding conditional logic and basic operations. Mastering such problems helps build a strong foundation in programming.

    Additional Resources

    Keep going with JavaAbout 340 step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free