We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.

Great solution. You could also do away with the 'i' variable and the while loop by replacing them with a for loop.

n = int(input())
arr = list(map(int, input().split()))
largest = max(arr)
for i in range(n):
if largest == max(arr):
arr.remove(max(arr))
print(max(arr))

Your code will show error because when largest number is removed in one iteration, then the next number will be assigned it's index and thus it will become a mess. you could try this

n = int(input())
arr = list(map(int, input().split()))
a = max(arr)
for x in range(len(arr)-1,-1,-1):
if arr[x] == a:
arr.remove(arr[x])
print(max(arr))

n = int(input())
arr = list(map(int, input().split()))
listnew=[]
for i in arr:
if i not in listnew:
listnew.append(i)
listnew.sort(reverse=True)
print(listnew[1])

When I tested a few custom test cases with multiple custom values and they passed. Unless, you think there's a test case I missed? If so, I'm curious which test case would this fail?

n = int(input())
arr = list(map(int, input().split()))
listnew=[]
for i in arr:
if i not in listnew:
listnew.append(i)
listnew.sort(reverse=True)
print(listnew[1])

It seems to me that your code is O(n^2) in the worst case due to the following reasoning:
-Assume we have the worst case like [5,6,6,6,...,6]- an array consisting almost fully out of sixs.
-Then your for-loop will work n-times calling max(arr)-function, which takes O(n).
-So we can say that it's O(n^2) in the worst case, while I can provide the solution with O(n) in this case, even though it's less elegant and requires the creation of some more temporary variables to store the max and second highest value.
-Then can you still claim that your code is the most effective? Please, correct me if I'm wrong, I would really appreciate that.

this code willl get error remove function remove the index values not the max value to remove you want to caluclate the max value index and we want to remove it.

## Find the Runner-Up Score!

You are viewing a single comment's thread. Return to all comments →

you do have a point, max() is O(n), it's not very efficient if there are many duplicated max values

@tao_zhang, here it is for many duplicated max values -

first=arr[0] second=0 for i in range(1,n): if first

max and remove both are O(n)

Great solution. You could also do away with the

'i'variable and thewhile loopby replacing them with afor loop.edit: fixed logical error

Your code looks fine but there is a mistake. Try to figure out what it is.

oops, nice catch. I must have copied and pasted the code I was messing with rather than the finished code. It's fixed now.

ok good kuck

he figured out that

Your code will show error because when largest number is removed in one iteration, then the next number will be assigned it's index and thus it will become a mess. you could try this

What were the examples that you used that raised an error? I'd like to check it out.

## your error wont occurs here

if

name== 'main':hey I am new to python here.can u tell me what is significance of map keyword while taking inputs.

python map() is an iterator.so that we can iterate over elements. can be converted to tuple ,lists

map is also very usefull to use lambda functinons and it applies function to all items in the input list

yes!!one more point!!

it wont work for multiple max value...

When I tested a few custom test cases with multiple custom values and they passed. Unless, you think there's a test case I missed? If so, I'm curious which test case would this fail?

very nice code . Thanks

can you plz, tell me what was the error in your code before fixing it.

Thanks for this! Very clear

## use it

if

name== 'main':Simple and super

can anyone please explain this code

It seems to me that your code is O(n^2) in the worst case due to the following reasoning: -Assume we have the worst case like [5,6,6,6,...,6]- an array consisting almost fully out of sixs. -Then your for-loop will work n-times calling max(arr)-function, which takes O(n). -So we can say that it's O(n^2) in the worst case, while I can provide the solution with O(n) in this case, even though it's less elegant and requires the creation of some more temporary variables to store the max and second highest value. -Then can you still claim that your code is the most effective? Please, correct me if I'm wrong, I would really appreciate that.

the line 6 is error

Traceback (most recent call last): File "solution.py", line 7, in res=max(arr) ValueError: max() arg is an empty sequence

I am also getting same error. however its res==max(arr)

this code willl get error remove function remove the index values not the max value to remove you want to caluclate the max value index and we want to remove it.

can u plz keep the snippet of ur code?

can anyone explain this code

use set instead of list

Thank u for your solution...It is very helpful for python beginner like me

Yes, alternatively you can use sort function at start and then pop elements until it is not equal to first popped element.

just use set instead of list...it will save only one value for multiple values ..