用于不同元素数组中第三大元素的Python程序
给定一个包含 n 个整数的数组,找到第三大元素。数组中的所有元素都是不同的整数。
例子 :
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
朴素方法:任务是首先找到最大的元素,然后是第二大的元素,然后排除它们都找到第三大的元素。其基本思想是对数组进行两次迭代,标记最大和第二大元素,然后排除它们都找到第三大元素,即排除最大值和第二大元素的最大元素。
- 算法:
- 首先,遍历数组并找到最大值。
- 将此作为第一个最大值与其索引一起存储。
- 现在遍历整个数组找到第二个最大值,不包括最大元素。
- 最后第三次遍历数组,找到第三个最大的元素,即排除最大值和第二个最大值。
Python3
# 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
Python3
# 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
- 输出:
The third Largest element is 13
- 复杂性分析:
- 时间复杂度: O(n)。
由于数组迭代三次并在恒定时间内完成 - 空间复杂度: O(1)。
不需要额外的空间,因为索引可以存储在常量空间中。
- 时间复杂度: O(n)。
有效方法:问题涉及在一次遍历中找到数组中的第三大元素。这个问题可以通过一个类似的问题来解决——找到第二个最大元素。所以想法是从头到尾遍历数组,并跟踪直到该索引的三个最大元素(存储在变量中) 。因此,在遍历整个数组之后,变量将存储数组中三个最大元素的索引(或值) 。
- 算法:
- 创建三个变量, first , second , third ,以存储数组中三个最大元素的索引。 (最初它们都被初始化为最小值)。
- 沿着输入数组从开始到结束移动。
- 对于每个索引,检查元素是否大于第一个。如果元素较大,则更新first的值,并将first的值分配给second ,将second的值分配给third 。所以最大的元素被更新,之前存储为最大的元素变成第二大元素,第二大元素变成第三大元素。
- 否则,如果元素大于第二个,则更新第二个的值,第二大的元素变为第三大。
- 如果前两个条件失败,但元素大于第三个,则更新第三个。
- 从头到尾遍历数组后打印第三个的值
Python3
# 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
- 输出:
The third Largest element is 13
- 复杂性分析:
- 时间复杂度: O(n)。
由于数组迭代一次并在恒定时间内完成 - 空间复杂度: O(1)。
不需要额外的空间,因为索引可以存储在常量空间中。
- 时间复杂度: O(n)。
有关详细信息,请参阅有关不同元素数组中第三大元素的完整文章!