Open In App
Related Articles

Python program to find the Decreasing point in List

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Report issue
Report

Given a list, get the index of element where the list shows the first negative trend, i.e first point where the next element < current element. If not found return -1.

Input : test_list = [3, 6, 8, 9, 12, 5, 18, 1] 
Output : 4 
Explanation : At 12 -> 5, first decreasing point occurs.


Input : test_list = [3, 9, 12, 5, 18, 1] 
Output : 2 
Explanation : At 12 -> 5, first decreasing point occurs. 

Method #1 : Using loop

In this, we check for the next element to be less than the current element, at a point where it is first found, we break the loop. 

Python3

# Python3 code to demonstrate working of
# Decreasing point in List
# Using loop
 
# initializing list
test_list = [3, 6, 8, 9, 12, 5, 18, 1]
 
# printing original list
print("The original list is : " + str(test_list))
 
res = -1
for idx in range(0, len(test_list) - 1):
     
    # checking for 1st decreasing element
    if test_list[idx + 1] < test_list[idx]:
        res = idx
        break
 
# printing result
print("Decreasing Point : " + str(res))

                    

Output:

The original list is : [3, 6, 8, 9, 12, 5, 18, 1]
Decreasing Point : 4

Time Complexity: O(n)
Auxiliary Space: O(1)

Method #2 : Using enumerate() + loop

In this, we check for index and value simultaneously using enumerate, a similar method to above, the difference being separate index element access.

Python3

# Python3 code to demonstrate working of
# Decreasing point in List
# Using enumerate() + loop
 
# initializing list
test_list = [3, 6, 8, 9, 12, 5, 18, 1]
 
# printing original list
print("The original list is : " + str(test_list))
 
res = -1
for idx, ele in enumerate(test_list):
     
    # checking for 1st decreasing element
    if test_list[idx + 1] < ele:
        res = idx
        break
 
# printing result
print("Decreasing Point : " + str(res))

                    

 
Output:

The original list is : [3, 6, 8, 9, 12, 5, 18, 1]
Decreasing Point : 4

Time Complexity: O(n)
Auxiliary Space: O(n)

Method 3 :  using recursion

  1. The program defines a function called “decreasing_point” which takes a list “lst” as an argument.
  2. The first “if” condition checks if the length of the list is less than or equal to 1. If it is, the function returns -1.
  3. The second “if” condition checks if the first element of the list is greater than the second element. If it is, it means the decreasing point is at index 0, so the function returns 0.
  4. If neither of the above conditions is true, the function makes a recursive call to itself with the list sliced from index 1 to the end (lst[1:]). This means that the recursive call is made with a smaller list than the original list.
  5. The result of the recursive call is stored in a variable called “res”.
  6. If “res” is not equal to -1 (which means the decreasing point was found in the sublist), the function returns “res + 1”, which is the index of the decreasing point in the original list.
  7. If “res” is equal to -1 (which means the decreasing point was not found in the sublist), the function returns -1.
  8. The program initializes a list called “test_list” and prints it.
  9. The program calls the “decreasing_point” function with the “test_list” and stores the result in a variable called “res”.
  10. The program prints the result, which is the index of the decreasing point in the original list.

Python3

# Python3 code to demonstrate working of
# Decreasing point in List
# Using recursion
 
def decreasing_point(lst):
    if len(lst) <= 1:
        return -1
    if lst[0] > lst[1]:
        return 0
    res = decreasing_point(lst[1:])
    return res + 1 if res != -1 else -1
 
# initializing list
test_list = [3, 6, 8, 9, 12, 5, 18, 1]
 
# printing original list
print("The original list is : " + str(test_list))
 
# calling decreasing_point function
res = decreasing_point(test_list)
 
# printing result
print("Decreasing Point : " + str(res))

                    

Output
The original list is : [3, 6, 8, 9, 12, 5, 18, 1]
Decreasing Point : 4

The time complexity of this function is O(n), where n is the length of the input list, since it processes each element of the list only once. 

The auxiliary space complexity is O(n), since it uses recursion and each recursive call adds a new stack frame to the call stack.



Last Updated : 29 Apr, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads