I have written the python code for this problem. And the code has all the necessary explanation in the comments.
# idea : each element is compared to other elements during
# array_max exploration (n-1 comparison)
# during this exploration, a list is stored as follows:
# list=[k,value,a,b,c,d....]
# where k is # of elements with which 'value' is compared during the search for array_max.
# and a,b,c,d are the compared elements.
# so to find the second largest element we need to search
# array_max in the list where
# value=array_max. and at max k will be log(n).
def getmax(l,r,a):
if lcomparedright[1]:
comparedleft[0]+=1
comparedleft.append(comparedright[1])
return comparedleft
else: #if max_value is in right sub-part
comparedright[0]+=1
comparedright.append(comparedleft[1])
return comparedright
#if len(subpart)==1. Generating list to store
#compared_elements (with a[l](an element from array))
else:
compared=[1,int(a[l])]
return compared
def secondmax(a):
if len(a)==2:
if a[0]>a[1]:
return a[1]
else:
return a[0];
list1=getmax(0,len(a)-1,a) # n-1 comparison
list2=getmax(2,list1[0],list1) # log(n)-1 comparison because
# list will contain log(n) element
# from [2:k]
return list2[1] # return the max_value of
# list1[2:k]
list1=input("enter the array = ").split()
if len(list1)==1:
print("give array with more than 2 elements")
else:
c=secondmax(list1)
print(c)