- Related Questions & Answers
- Find N distinct numbers whose bitwise Or is equal to K in C++
- Find unique pairs such that each element is less than or equal to N in C++
- Count of subarrays whose maximum element is greater than k in C++
- C++ program to find unique pairs such that each element is less than or equal to N
- Adding elements of an array until every element becomes greater than or equal to k in C++.
- Find N distinct numbers whose bitwise Or is equal to K in Python
- Count numbers whose XOR with N is equal to OR with N in C++
- How to find numbers in an array that are greater than, less than, or equal to a value in java?
- Find all factorial numbers less than or equal to n in C++
- Find frequency of each element in a limited range array in less than O(n) time in C++
- Count pairs in a sorted array whose product is less than k in C++
- Count pairs in a sorted array whose sum is less than x in C++
- C++ program to Adding elements of an array until every element becomes greater than or equal to k
- Find the element having different frequency than other array elements in C++
- Find Equal (or Middle) Point in a sorted array with duplicates in C++

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

Consider we have an array with size n. This array is sorted. There is one element whose frequency is greater than or equal to n/2, where n is the number of elements in the array. So if the array is like [3, 4, 5, 5, 5], then the output will be 5.

If we closely observe these type of array, we can easily notice that the number whose frequency is greater than or equal to n/2, will be present at index n/2 also. So the element can be found at position 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

Advertisements