
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Find element in a sorted array whose frequency is greater than or equal to n/2 in C++.
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
Example
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"; }
Output −
The number 4 has occurred more than or equal to 10/2 amount of times
- Related Articles
- Check which element in a masked array is greater than or equal to a given value in NumPy
- How to find the frequency of values greater than or equal to a certain value in R?
- Adding elements of an array until every element becomes greater than or equal to k in C++.
- Find unique pairs such that each element is less than or equal to N in C++
- First element greater than or equal to X in prefix sum of N numbers using Binary Lifting in C++
- Find the number of elements greater than k in a sorted array using C++
- Find Multiples of 2 or 3 or 5 less than or equal to N in C++
- Count elements less than or equal to a given value in a sorted rotated array in C++
- How to find numbers in an array that are greater than, less than, or equal to a value in java?
- Find N distinct numbers whose bitwise Or is equal to K in C++
- Mask array elements greater than or equal to a given value in Numpy
- Find frequency of each element in a limited range array in less than O(n) time in C++
- Find Equal (or Middle) Point in a sorted array with duplicates in C++
- Count of subarrays whose maximum element is greater than k in C++
- C++ program to Adding elements of an array until every element becomes greater than or equal to k

Advertisements