在C ++中,在频率大于或等于n / 2的排序数组中查找元素。
考虑我们有一个大小为n的数组。该数组已排序。有一个元素的频率大于或等于n/2,其中n是数组中元素的数量。因此,如果数组类似于[3,4,5,5,5],则输出将为5。
如果我们仔细观察这些类型的数组,我们可以很容易地注意到,频率大于或等于n/2的数字也会出现在索引n/2处。因此该元素可以在位置n/2处找到
示例
Source Code:
#include<iostream>
using namespace std;
int higherFreq(int arr[], int n) {
return arr[n / 2];
}
int main() {
int arr[] = { 1, 2, 3, 4 , 4, 4, 4, 4, 4, 5};
int n = sizeof(arr) / sizeof(arr[0]);
cout << "The number " << higherFreq(arr, n) << " has occurred more than or equal to "<<n <<"/2 amount of times";
}输出-
The number 4 has occurred more than or equal to 10/2 amount of times
热门推荐
10 新年门口花束祝福语简短
11 盘锦结婚祝福语大全简短
12 父母生日祝福语 简短独特
13 家庭恩爱祝福语简短英文
14 高考俄语祝福语大全简短
15 虎年祝福语 诗句唯美简短
16 生日婚礼祝福语简短精辟
17 虎年喝酒拜年祝福语简短
18 教师闺蜜祝福语简短