给定一个由N 个正整数组成的数组arr[] ,任务是找到至少大于数组元素一半的元素。
例子:
Input: arr[] = {1, 6, 3, 4}
Output: 4 6
Explanation:
Size of the array is 4. The elements which are greater than atleast N/2(= 4/2 = 2) elements of the array are 4 and 6.
Input: arr[] = {10, 4, 2, 8, 9}
Output: 10 9 8
Naive Approach:关于Naive Approach,请参考本文的前一篇文章。
时间复杂度: O(N 2 )
辅助空间: O(1)
基于排序的方法:有关基于排序的方法,请参阅本文的前一篇文章。
时间复杂度: O(N*log N)
辅助空间: O(1)
方法:通过跟踪数组元素的计数,也可以使用哈希来优化上述方法。找到每个元素的频率后,按非递增顺序打印元素,直到打印了N/2 个元素。 请按照以下步骤解决问题:
- 初始化一个变量,比如mid as (N + 1)/2来存储数组的中间索引。
- 初始化一个变量,比如max来存储数组的最大元素。
- 初始化一个数组count[]来存储每个元素的频率。
- 遍历数组arr[]并将count[arr[i]]增加 1。
- 遍历 数组count[]使用后面的变量i并执行以下步骤:
- 迭代直到 count[i] 的值为正,然后执行以下步骤:
- 打印i的值并将count[i]和mid的值减1 。
- 如果mid 的值为0 ,则跳出循环。
- 如果mid 的值为0 ,则跳出循环。
- 迭代直到 count[i] 的值为正,然后执行以下步骤:
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to find the element that
// are larger than half of elements
// of the array
void findLarger(int arr[], int n)
{
// Find the value of mid
int mid = (n + 1) / 2;
// Stores the maximum element
int mx = *max_element(arr, arr + n);
// Stores the frequency of each
// array element
int count[mx + 1] = { 0 };
for (int i = 0; i < n; i++) {
count[arr[i]]++;
}
// Traverse the array in the
// reverse order
for (int i = mx; i >= 0; i--) {
while (count[i] > 0) {
// Decrement the value
// of count[i] and mid
count[i]--;
mid--;
// Print the current element
cout << i << ' ';
// Check if the value of
// mid is equal to 0
if (mid == 0)
break;
}
if (mid == 0)
break;
}
}
// Driver Code
int main()
{
int arr[] = { 10, 4, 2, 8, 9 };
int N = sizeof(arr) / sizeof(arr[0]);
findLarger(arr, N);
return 0;
}
Java
// Java program for the above approach
import java.util.*;
class GFG{
// Function to find the element that
// are larger than half of elements
// of the array
static void findLarger(int arr[], int n)
{
// Find the value of mid
int mid = (n + 1) / 2;
// Stores the maximum element
int mx = Arrays.stream(arr).max().getAsInt();
// Stores the frequency of each
// array element
int count[] = new int[mx+1];
for (int i = 0; i < mx+1; i++) {
count[i] = 0;
}
for (int i = 0; i < n; i++) {
count[arr[i]]++;
}
// Traverse the array in the
// reverse order
for (int i = mx; i >= 0; i--) {
while (count[i] > 0) {
// Decrement the value
// of count[i] and mid
count[i]--;
mid--;
// Print the current element
System.out.print(i + " ");
// Check if the value of
// mid is equal to 0
if (mid == 0)
break;
}
if (mid == 0)
break;
}
}
// Driver Code
public static void main(String[] args)
{
int arr[] = { 10, 4, 2, 8, 9 };
int N = arr.length;
findLarger(arr, N);
}
}
// This code is contributed by sanjoy_62.
Python3
# Python program for the above approach
# Function to find the element that
# are larger than half of elements
# of the array
def findLarger(arr, n):
# Find the value of mid
mid = (n + 1) // 2
# Stores the maximum element
mx = max(arr)
# Stores the frequency of each
# array element
count = [0]*(mx + 1)
for i in range(n):
count[arr[i]] += 1
# Traverse the array in the
# reverse order
for i in range(mx,-1, -1):
while (count[i] > 0):
# Decrement the value
# of count[i] and mid
count[i] -= 1
mid -= 1
# Prthe current element
print(i, end = " ")
# Check if the value of
# mid is equal to 0
if (mid == 0):
break
if (mid == 0):
break
# Driver Code
if __name__ == '__main__':
arr = [10, 4, 2, 8, 9 ]
N = len(arr)
findLarger(arr, N)
# This code is contributed by mohit kumar 29.
C#
// C# program for the above approach
using System;
using System.Collections.Generic;
class GFG{
// Function to find the element that
// are larger than half of elements
// of the array
static void findLarger(int []arr, int n)
{
// Find the value of mid
int mid = (n + 1) / 2;
// Stores the maximum element
int mx = Int32.MinValue;
for(int i=0;imx)
mx = arr[i];
}
// Stores the frequency of each
// array element
int []count = new int[mx + 1];
Array.Clear(count,0,mx+1);
for (int i = 0; i < n; i++) {
count[arr[i]]++;
}
// Traverse the array in the
// reverse order
for (int i = mx; i >= 0; i--) {
while (count[i] > 0) {
// Decrement the value
// of count[i] and mid
count[i]--;
mid--;
// Print the current element
Console.Write(i+" ");
// Check if the value of
// mid is equal to 0
if (mid == 0)
break;
}
if (mid == 0)
break;
}
}
// Driver Code
public static void Main()
{
int []arr = { 10, 4, 2, 8, 9 };
int N = arr.Length;
findLarger(arr, N);
}
}
// This code is contributed by SURENDRA_GANGWAR.
Javascript
输出:
10 9 8
时间复杂度: O(M),其中 M 是数组的最大元素, arr[]
辅助空间: O(M)
如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程和学生竞争性编程现场课程。