用于不同元素数组中第三大元素的 C++ 程序
给定一个包含 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
朴素方法:任务是首先找到最大的元素,然后是第二大的元素,然后排除它们都找到第三大的元素。其基本思想是对数组进行两次迭代,标记最大和第二大元素,然后排除它们都找到第三大元素,即排除最大值和第二大元素的最大元素。
- 算法:
- 首先,遍历数组并找到最大值。
- 将此作为第一个最大值与其索引一起存储。
- 现在遍历整个数组找到第二个最大值,不包括最大元素。
- 最后第三次遍历数组,找到第三个最大的元素,即排除最大值和第二个最大值。
C++
// C++ program to find third Largest
// element in an array of distinct elements
#include
void thirdLargest(int arr[], int arr_size)
{
/* There should be atleast three elements */
if (arr_size < 3)
{
printf(" Invalid Input ");
return;
}
// Find first largest element
int first = arr[0];
for (int i = 1; i < arr_size ; i++)
if (arr[i] > first)
first = arr[i];
// Find second largest element
int second = INT_MIN;
for (int i = 0; i < arr_size ; i++)
if (arr[i] > second && arr[i] < first)
second = arr[i];
// Find third largest element
int third = INT_MIN;
for (int i = 0; i < arr_size ; i++)
if (arr[i] > third && arr[i] < second)
third = arr[i];
printf("The third Largest element is %d
", third);
}
/* Driver program to test above function */
int main()
{
int arr[] = {12, 13, 1, 10, 34, 16};
int n = sizeof(arr)/sizeof(arr[0]);
thirdLargest(arr, n);
return 0;
}
C++
// C++ program to find third
// Largest element in an array
#include
void thirdLargest(int arr[], int arr_size)
{
/* There should be atleast three elements */
if (arr_size < 3)
{
printf(" Invalid Input ");
return;
}
// Initialize first, second and third Largest element
int first = arr[0], second = INT_MIN, third = INT_MIN;
// Traverse array elements to find the third Largest
for (int i = 1; i < arr_size ; i ++)
{
/* 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 */
else if (arr[i] > second)
{
third = second;
second = arr[i];
}
/* If arr[i] is in between second and third */
else if (arr[i] > third)
third = arr[i];
}
printf("The third Largest element is %d
", third);
}
/* Driver program to test above function */
int main()
{
int arr[] = {12, 13, 1, 10, 34, 16};
int n = sizeof(arr)/sizeof(arr[0]);
thirdLargest(arr, n);
return 0;
}
- 输出:
The third Largest element is 13
- 复杂性分析:
- 时间复杂度: O(n)。
由于数组迭代三次并在恒定时间内完成 - 空间复杂度: O(1)。
不需要额外的空间,因为索引可以存储在常量空间中。
- 时间复杂度: O(n)。
有效方法:问题涉及在一次遍历中找到数组中的第三大元素。这个问题可以通过一个类似的问题来解决——找到第二个最大元素。所以想法是从头到尾遍历数组,并跟踪直到该索引的三个最大元素(存储在变量中) 。因此,在遍历整个数组之后,变量将存储数组中三个最大元素的索引(或值) 。
- 算法:
- 创建三个变量, first , second , third ,以存储数组中三个最大元素的索引。 (最初它们都被初始化为最小值)。
- 沿着输入数组从开始到结束移动。
- 对于每个索引,检查元素是否大于第一个。如果元素较大,则更新first的值,并将first的值分配给second ,将second的值分配给third 。所以最大的元素被更新,之前存储为最大的元素变成第二大元素,第二大元素变成第三大元素。
- 否则,如果元素大于第二个,则更新第二个的值,第二大的元素变为第三大。
- 如果前两个条件失败,但元素大于第三个,则更新第三个。
- 从头到尾遍历数组后打印第三个的值
C++
// C++ program to find third
// Largest element in an array
#include
void thirdLargest(int arr[], int arr_size)
{
/* There should be atleast three elements */
if (arr_size < 3)
{
printf(" Invalid Input ");
return;
}
// Initialize first, second and third Largest element
int first = arr[0], second = INT_MIN, third = INT_MIN;
// Traverse array elements to find the third Largest
for (int i = 1; i < arr_size ; i ++)
{
/* 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 */
else if (arr[i] > second)
{
third = second;
second = arr[i];
}
/* If arr[i] is in between second and third */
else if (arr[i] > third)
third = arr[i];
}
printf("The third Largest element is %d
", third);
}
/* Driver program to test above function */
int main()
{
int arr[] = {12, 13, 1, 10, 34, 16};
int n = sizeof(arr)/sizeof(arr[0]);
thirdLargest(arr, n);
return 0;
}
- 输出:
The third Largest element is 13
- 复杂性分析:
- 时间复杂度: O(n)。
由于数组迭代一次并在恒定时间内完成 - 空间复杂度: O(1)。
不需要额外的空间,因为索引可以存储在常量空间中。
- 时间复杂度: O(n)。
有关详细信息,请参阅有关不同元素数组中第三大元素的完整文章!