Python program to find the Decreasing point in List
Last Updated :
29 Apr, 2023
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
- The program defines a function called "decreasing_point" which takes a list "lst" as an argument.
- 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.
- 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.
- 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.
- The result of the recursive call is stored in a variable called "res".
- 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.
- If "res" is equal to -1 (which means the decreasing point was not found in the sublist), the function returns -1.
- The program initializes a list called "test_list" and prints it.
- The program calls the "decreasing_point" function with the "test_list" and stores the result in a variable called "res".
- 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))
OutputThe 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.
Similar Reads
Python program to find smallest number in a list In this article, we will discuss various methods to find smallest number in a list. The simplest way to find the smallest number in a list is by using Python's built-in min() function.Using min()The min() function takes an iterable (like a list, typle etc.) and returns the smallest value.Pythona = [
2 min read
Python Program to find the Next Nearest element in a Matrix Given a matrix, a set of coordinates and an element, the task is to write a python program that can get the coordinates of the elements next occurrence. Input : test_list = [[4, 3, 1, 2, 3], [7, 5, 3, 6, 3], [8, 5, 3, 5, 3], [1, 2, 3, 4, 6]], i, j = 1, 3, K = 3 Output : (1, 4) Explanation : After (1
4 min read
Python program to Test if all y occur after x in List Given a List, test if all occurrences of y are after the occurrence of x in the list. Input : test_list = [4, 5, 6, 2, 4, 5, 2, 9], x, y = 6, 2 Output : True Explanation : All occurrences of 2 are after 6, hence true. Input : test_list = [4, 2, 5, 6, 2, 4, 5, 2, 9], x, y = 6, 2 Output : False Explan
4 min read
Python program to find Successive row difference in Matrix Given a Matrix, the task is to write a Python program to perform differences from the previous row on the basis of the elements present. Input : test_list = [[5, 6, 3, 1], [7, 5, 3, 1], [3, 2], [7, 3, 3, 2], [2, 3], [9, 8, 1]] Output : [[], [7], [2], [7], [], [8, 9, 1]] Explanation : Comparing 1st a
7 min read
Python program to create a list centered on zero Given two integer variables, limit and diff, write a Python program to create a list that is centered on zero, using limit, which specifies limit of the list and diff that specifies the common difference between integers. Examples: Input : limit = 1, diff = 0.5 Output : [-1, -0.5, 0.0, 0.5, 1] Input
3 min read