How do you find the third largest smallest number in a list in Python?

Python | Largest, Smallest, Second Largest, Second Smallest in a List

Since, unlike other programming languages, Python does not have arrays, instead, it has list. Using lists is more easy and comfortable to work with in comparison to arrays. Moreover, the vast inbuilt functions of Python, make the task easier. So using these techniques, let’s try to find the various ranges of the number in a given list.
Examples:

Input : list = [12, 45, 2, 41, 31, 10, 8, 6, 4] Output : Largest element is: 45 Smallest element is: 2 Second Largest element is: 41 Second Smallest element is: 4 Input : list = [22, 85, 62, 40, 55, 12, 39, 2, 43] Output : Largest element is: 85 Smallest element is: 2 Second Largest element is: 62 Second Smallest element is: 12

Python Program for Third largest element in an array of distinct elements

Given an array of n integers, find the third largest element. All the elements in the array are distinct integers.
Example :

Input: arr[] = {1, 14, 2, 16, 10, 20} Output: The third Largest element is 14 Explanation: Largest element is 20, second largest element is 16 and third largest element is 14 Input: arr[] = {19, -10, 20, 14, 2, 16, 10} Output: The third Largest element is 16 Explanation: Largest element is 20, second largest element is 19 and third largest element is 16

Recommended: Please solve it on “PRACTICE” first, before moving on to the solution.

Naive Approach: The task is to first find the largest element, followed by the second-largest element and then excluding them both find the third-largest element. The basic idea is to iterate the array twice and mark the maximum and second maximum element and then excluding them both find the third maximum element, i.e the maximum element excluding the maximum and second maximum.

  • Algorithm:
    1. First, iterate through the array and find maximum.
    2. Store this as first maximum along with its index.
    3. Now traverse the whole array finding the second max, excluding the maximum element.
    4. Finally traverse the array the third time and find the third largest element i.e., excluding the maximum and second maximum.




# Python 3 program to find
# third Largest element in
# an array of distinct elements
import sys
def thirdLargest(arr, arr_size):
# There should be
# atleast three elements
if (arr_size < 3):
print(" Invalid Input ")
return
# Find first
# largest element
first = arr[0]
for i in range(1, arr_size):
if (arr[i] > first):
first = arr[i]
# Find second
# largest element
second = -sys.maxsize
for i in range(0, arr_size):
if (arr[i] > second and
arr[i] < first):
second = arr[i]
# Find third
# largest element
third = -sys.maxsize
for i in range(0, arr_size):
if (arr[i] > third and
arr[i] < second):
third = arr[i]
print("The Third Largest",
"element is", third)
# Driver Code
arr = [12, 13, 1,
10, 34, 16]
n = len(arr)
thirdLargest(arr, n)
# This code is contributed
# by Smitha
  • Output:
The third Largest element is 13
  • Complexity Analysis:
    • Time Complexity: O(n).
      As the array is iterated thrice and is done in a constant time
    • Space complexity: O(1).
      No extra space is needed as the indices can be stored in constant space.

Efficient Approach: The problem deals with finding the third largest element in the array in a single traversal. The problem can be cracked by taking help of a similar problem- finding the second maximum element. So the idea is to traverse the array from start to end and to keep track of the three largest elements up to that index (stored in variables). So after traversing the whole array, the variables would have stored the indices (or value) of the three largest elements of the array.

  • Algorithm:
    1. Create three variables, first, second, third, to store indices of three largest elements of the array. (Initially all of them are initialized to a minimum value).
    2. Move along the input array from start to the end.
    3. For every index check if the element is larger than first or not. Update the value of first, if the element is larger, and assign the value of first to second and second to third. So the largest element gets updated and the elements previously stored as largest becomes second largest, and the second largest element becomes third largest.
    4. Else if the element is larger than the second, then update the value of second,and the second largest element becomes third largest.
    5. If the previous two conditions fail, but the element is larger than the third, then update the third.
    6. Print the value of third after traversing the array from start to end




# Python3 program to find
# third Largest element in
# an array
import sys
def thirdLargest(arr, arr_size):
# There should be
# atleast three elements
if (arr_size < 3):
print(" Invalid Input ")
return
# Initialize first, second
# and third Largest element
first = arr[0]
second = -sys.maxsize
third = -sys.maxsize
# Traverse array elements
# to find the third Largest
for i in range(1, arr_size):
# If current element is
# greater than first,
# then update first,
# second and third
if (arr[i] > first):
third = second
second = first
first = arr[i]
# If arr[i] is in between
# first and second
elif (arr[i] > second):
third = second
second = arr[i]
# If arr[i] is in between
# second and third
elif (arr[i] > third):
third = arr[i]
print("The third Largest" ,
"element is", third)
# Driver Code
arr = [12, 13, 1,
10, 34, 16]
n = len(arr)
thirdLargest(arr, n)
# This code is contributed
# by Smitha
  • Output:
The third Largest element is 13
  • Complexity Analysis:
    • Time Complexity: O(n).
      As the array is iterated once and is done in a constant time
    • Space complexity: O(1).
      No extra space is needed as the indices can be stored in constant space.

Please refer complete article on Third largest element in an array of distinct elements for more details!

How do you find the third largest smallest number in a list in Python?




Article Tags :
Arrays
Python
Python Programs
Searching
Amazon
Practice Tags :
Amazon
Arrays
Searching

Python program to find the largest and smallest number in a list

Hello everybody, this is a Python program which finds out the smallest and largest number in the list.

How do you find the third largest smallest number in a list in Python?
Smallest and Largest number in list — programminginpython.com

Here we use 2 predefined functions min() and max() which check for the smallest and largest number in a list respectively.

Python Program to find the Largest and Smallest Number in a List Example 1

Thispython programallows user to enter the length of a List. Next, we usedFor Loopto add numbers to the list. Here, the minand max functions in Pythonreturns the smallest and largest numbers or minimum and maximum values in a List.

# Python Program to find Largest and Smallest Number in a List NumList = [] Number = int(input("Please enter the Total Number of List Elements: ")) for i in range(1, Number + 1): value = int(input("Please enter the Value of %d Element : " %i)) NumList.append(value) print("The Smallest Element in this List is : ", min(NumList)) print("The Largest Element in this List is : ", max(NumList))

Python largest and smallest list number output

Please enter the Total Number of List Elements: 5 Please enter the Value of 1 Element : 50 Please enter the Value of 2 Element : 45 Please enter the Value of 3 Element : 33 Please enter the Value of 4 Element : 78 Please enter the Value of 5 Element : 66 The Smallest Element in this List is : 33 The Largest Element in this List is : 78

Python program to find largest and smallest elements in a list

Python program to find largest and smallest elements in a list

Contents

  • 1 Python program to find largest and smallest elements in a list
    • 1.1 To find the largest and smallest number in an integer list in Python
    • 1.2 To find the largest and smallest number in a float list in Python
    • 1.3 find the largest and smallest number in the list using max() and min() function
    • 1.4 Related posts:

In this tutorial, we will discuss the concept of Python program to find largest and smallest elements in the list

In this post, we will learn how to find the largest and smallest elements of a list in Python programming language

Python program to find largest and smallest elements in a list

To find the largest and smallest number in an integer list in Python

Program 1

In this programs, we can see step by step approach for completion of the program.

  • Create a number list in Python
  • Sort the list in ascending order
  • Display last elements in the list for largest
  • Display first elements in the list for smallest
#pyton program to find smallest elements in a list #create a list of elements(number) number=[54,67,43,89,21,34] #sorting the list number.sort() #print the last element in the list for largest print("largest elements is: ",number[5]) #print the first element in the list for smallest print("Smallest elements is: ",number[0])

When the above code is executed, it produces the following results

largest elements is: 89 Smallest elements is: 21

Program 2

Takes input from the user

In this programs, we can see step by step approach for completion of the program.

  • Create an empty list in Python
  • Takes the input to add the number of elements in the list from the user
  • Takes the input elements of the list one by one from the user
  • read each number and append each number to the list using for loop
  • sort the list elements in ascending order
  • Displays last and first elements for largest and smallest

#pyton program to find largest smallest elements in a list #create a empty list of elements(number) number=[] #asking number of elements to add in the list n=int(input("Enter the number of elements in list: ")) for i in range(1, n + 1): element=int(input("Enter elements: ")) number.append(element) #sort the list number.sort() print("Largest element is:",number[n-1]) print("Smallest element is:",number[0])

When the above code is executed, it produces the following results

Enter the number of elements in list: 4 Enter elements: 34 Enter elements: 67 Enter elements: 87 Enter elements: 65 Largest element is: 87 Smallest element is: 34

This program gets “n” number of elements and Enter the elements of the list as input from the user. then, this program finds and displays the largest smallest elements from the list

To find the largest and smallest number in a float list in Python

Program 1

#pyton program to find smallest elements in a list #create a list of elements(number) number=[54.5,67.7,34.43,65.89,21.34] #sorting te list number.sort() #print the last element in the list for lrgest print("largest elements is: ",number[4]) #print the first element in the list for smallest print("Smallest elements is: ",number[0])

When the above code is executed, it produces the following results

largest elements is: 67.7 Smallest elements is: 21.34

Program 2

Takes input from the user

#pyton program to find largest smallest elements in a list #create a empty list of elements(number) number=[] #asking number of elements to add in the list n=int(input("Enter the number of elements in list: ")) for i in range(1, n + 1): element=float(input("Enter elements: ")) number.append(element) #sort the list number.sort() print("Largest element is:",number[n-1]) print("Smallest element is:",number[0])

When the above code is executed, it produces the following results

Enter the number of elements in list: 5 Enter elements: 34.2 Enter elements: 56.43 Enter elements: 67.89 Enter elements: 87.65 Enter elements: 48.7 Largest element is: 87.65 Smallest element is: 34.2

This program gets “n” number of elements and Enter the elements of the list as input from the user. then, this program finds and displays the largest smallest elements from the list.

find the largest and smallest number in the list using max() and min() function

In this programs, we can see step by step approach for completion of the program.

  • Create an empty list in Python
  • Takes the input to add the number of elements in the list from the user
  • Takes the input elements of the list one by one from the user
  • read each number and append each number to the list using for loop
  • predefined max() function use to find maximum value
  • predefined min() function use to find minimum value
  • Displays last and first elements for largest and smallest
listNum=[] num=int(input("How many numbers in list: ")) for n in range(num): numbers =int(input("Enter numbers: ")) listNum.append(numbers) print("Maximum elements in the list: ",max(listNum)) print("Minimum elements in the list: ",min(listNum))

When the above code is executed, it produces the following results

How many numbers in list: 5 Enter numbers: 36 Enter numbers: 76 Enter numbers: 98 Enter numbers: 23 Enter numbers: 67 Maximum elements in the list: 98 Minimum elements in the list: 23

Suggested for you

Operator in Python

For loop in Python

Python program to find Average of numbers in a list
Python program to find sum of elements in a list
Python program to calculate sum of odd and even numbers
Java program to compute the sum of digits in a given numbers
Python program find factorial of a number using recursion
Display even and odd numbers without if statement in C