Programming Examples
Python Program to find the successor and predecessor of the largest element in list
Python Program to find the successor and predecessor of the largest element in list
Solution
mylist=eval(input("Enter any List : "))
m=max(mylist)
mi=mylist.index(m)
#print(m," -> ",mi)
if mi>0:
print("Predecessor of Largest No. ",mylist[mi-1])
else:
print("No Predecessor Exist")
if mi<len(mylist)-1:
print("Successor of Largest No. ",mylist[mi+1])
else:
print("No Successor Exist")
Output/ Explanation:
Enter any List : [1,2,3,5,2,1]
Predecessor of Largest No. 3
Successor of Largest No. 2
>>>
======================
Enter any List : [1,2,3,4]
Predecessor of Largest No. 3
No Successor Exist
>>>
======================
Enter any List : [4,3,2,1]
No Predecessor Exist
Successor of Largest No. 3