Print Positive / Negative: Buggy Code in C++ (Time Complexity: O(n))


Inside the code editor we've tried to write a function that takes an array nums as argument and for every number in the array:

  • prints "negative" if that number is less than zero

  • or prints the number itself, if the number is greater than or equal to zero

So when we called the function for [2, -12, 0, 4, -5, 3], we expected it to print:

2
negative
0
4
negative
3

but it seems like we made some mistakes because when we run our code, it produces this output:

positive
-12
0
positive
-5
positive

Assignment:

Your task is to fix our loop such that it will print the desired output.

Understanding the Problem

The core challenge of this problem is to correctly identify and print whether each number in the array is negative or non-negative. This is a common task in data processing where we need to categorize or filter data based on certain conditions.

Potential pitfalls include misunderstanding the conditions for printing "negative" and the number itself, or incorrectly implementing the loop logic.

Approach

To solve this problem, we need to iterate through the array and check each number. If the number is less than zero, we print "negative". Otherwise, we print the number itself.

Let's start with a naive approach and then optimize it:

Naive Approach

The naive approach involves iterating through the array and using an if-else statement to check the condition for each number. This approach is straightforward but can be prone to logical errors if not implemented correctly.

Optimized Approach

The optimized approach is similar to the naive approach but ensures that the conditions are checked correctly and the appropriate output is printed. This approach is efficient with a time complexity of O(n), where n is the number of elements in the array.

Algorithm

Here is a step-by-step breakdown of the algorithm:

  1. Iterate through each element in the array.
  2. For each element, check if it is less than zero.
  3. If it is less than zero, print "negative".
  4. Otherwise, print the number itself.

Code Implementation


#include <iostream>
#include <vector>

void printPositiveNegative(const std::vector<int>& nums) {
    // Iterate through each number in the array
    for (int num : nums) {
        // Check if the number is less than zero
        if (num < 0) {
            // Print "negative" if the number is less than zero
            std::cout << "negative" << std::endl;
        } else {
            // Print the number itself if it is greater than or equal to zero
            std::cout << num << std::endl;
        }
    }
}

int main() {
    // Test the function with the given example
    std::vector<int> nums = {2, -12, 0, 4, -5, 3};
    printPositiveNegative(nums);
    return 0;
}

Complexity Analysis

The time complexity of this approach is O(n), where n is the number of elements in the array. This is because we iterate through each element exactly once.

The space complexity is O(1) as we are not using any additional space that scales with the input size.

Edge Cases

Potential edge cases include:

  • An empty array: The function should handle this gracefully without any errors.
  • All positive numbers: The function should print all numbers correctly.
  • All negative numbers: The function should print "negative" for all numbers.
  • Mixed positive, negative, and zero values: The function should correctly identify and print each value.

Testing

To test the solution comprehensively, we should include a variety of test cases:

  • Simple cases with a few positive and negative numbers.
  • Edge cases such as an empty array and arrays with all positive or all negative numbers.
  • Complex cases with a large number of elements to ensure performance.

Thinking and Problem-Solving Tips

When approaching such problems, it is important to:

  • Clearly understand the problem statement and requirements.
  • Break down the problem into smaller, manageable steps.
  • Consider edge cases and how to handle them.
  • Write clean, readable code with comments to explain your logic.
  • Test your solution with a variety of test cases to ensure correctness.

Conclusion

In this blog post, we discussed how to solve the problem of printing "negative" for negative numbers and the number itself for non-negative numbers in an array. We explored a naive approach and an optimized approach, provided a detailed algorithm, and implemented the solution in C++. We also analyzed the complexity and discussed edge cases and testing strategies.

Understanding and solving such problems is crucial for developing strong problem-solving skills and improving your coding abilities. Keep practicing and exploring different problems to enhance your skills further.

Additional Resources

For further reading and practice, consider the following resources: