Python | Counting sign change in list containing Positive and Negative Integers
Given a list containing Positive and Negative integers, We have to find number of times the sign(Positive or Negative) changes in the list.
Input: [-1, 2, 3, -4, 5, -6, 7, 8, -9, 10, -11, 12] Output:9 Explanation : Sign change from -1 to 2, ans = 1 Sign change from 3 to -4, ans = 2 Sign change from -4 to 5, ans = 3 Sign change from 5 to -6, ans = 4 Sign change from -6 to 7, ans = 5 Sign change from 8 to -9, ans = 6 Sign change from -9 to 10, ans = 7 Sign change from 10 to -11 ans = 8 Sign change from -11 to 12, ans = 9 Input: [-1, 2, 3, -4, 5, -11, 12] Output:5 Explanation : Sign change from -1 to 2, ans = 1 Sign change from 3 to -4, ans = 2 Sign change from -4 to 5, ans = 3 Sign change from 5 to -11, ans = 4 Sign change from -11 to 12, ans = 5
Let’s discuss certain ways in which this task is performed.
Method #1: Using Iteration Using Iteration to find number of time sign changes in the list.
Time complexity: O(n),We are iterating over the given list Input of length n just once. Therefore, the time complexity of the given code is O(n).
Space complexity: O(1),We have not used any extra space for the given code. Therefore, the space complexity is O(1).
Method #2: Using Itertools and groupby This is yet another way to perform this particular task using itertools.
Time Complexity: O(n), where n is the length of the list.
Space Complexity: O(n), where n is the length of the list. This is because we are creating a list of the same length as the input list in order to group the elements.
Method #3: Using Zip The most concise and readable way to find number of time sign changes in the list is using zip.
Time Complexity : O(N),The time complexity of the above code is O(N), because it iterates over the list once.
Space Complexity : O(1),The space complexity is O(1) because no additional space is used.
Method #4: Using recursion
This function uses recursion to iterate through the list and check for sign changes. The base case is an empty list, in which case it returns 0. Otherwise, it checks the sign of the first element and recurses with the rest of the list. It also checks for a sign change with the second element in the list and adds it to the recursive call’s result.
Time complexity for this approach is O(n) where n is the length of the list.
Auxiliary Space is O(n) for this approach, as the recursive calls are stacked and a new stack frame is created for each recursive call.
Method #5: Using Numpy Library.
- Convert the input list to a numpy array.
- Calculate the sign of each element of the array using np.sign() function.
- Compare the signs of adjacent elements using np.sign(Input[1:]) != np.sign(Input[:-1]).
- Count the number of True values in the above array using np.sum().
- Print the count as the answer.
Time Complexity: O(n), where n is the length of the input list. The np.sign() function and element-wise comparison using != are performed in O(n) time complexity. The np.sum() function takes O(1) time complexity in this case.
Space Complexity: O(n), as we are creating a numpy array of size n to store the input list.
Please Login to comment...