Find elements larger than half of the elements in an array | Set 2
Last Updated :
28 Dec, 2022
Given an array arr[] consisting of N positive integers, the task is to find the elements which are greater than at least half of the array elements.
Examples:
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: Please refer to the previous post of this article for the Naive Approach.
Time Complexity: O(N2)
Auxiliary Space: O(1)
Sorting-based Approach: Please refer to the previous post of this article for the Sorting Based Approach.
Time Complexity: O(N*log N)
Auxiliary Space: O(1)
Approach: The above approach can also be optimized by using Hashing by keeping track of the count of array elements. After finding the frequency of each element, print elements in non-increasing order till N/2 elements have been printed. Follow the steps below to solve the problem:
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void findLarger( int arr[], int n)
{
int mid = (n + 1) / 2;
int mx = *max_element(arr, arr + n);
int count[mx + 1] = { 0 };
for ( int i = 0; i < n; i++) {
count[arr[i]]++;
}
for ( int i = mx; i >= 0; i--) {
while (count[i] > 0) {
count[i]--;
mid--;
cout << i << ' ' ;
if (mid == 0)
break ;
}
if (mid == 0)
break ;
}
}
int main()
{
int arr[] = { 10, 4, 2, 8, 9 };
int N = sizeof (arr) / sizeof (arr[0]);
findLarger(arr, N);
return 0;
}
|
Java
import java.util.*;
class GFG{
static void findLarger( int arr[], int n)
{
int mid = (n + 1 ) / 2 ;
int mx = Arrays.stream(arr).max().getAsInt();
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]]++;
}
for ( int i = mx; i >= 0 ; i--) {
while (count[i] > 0 ) {
count[i]--;
mid--;
System.out.print(i + " " );
if (mid == 0 )
break ;
}
if (mid == 0 )
break ;
}
}
public static void main(String[] args)
{
int arr[] = { 10 , 4 , 2 , 8 , 9 };
int N = arr.length;
findLarger(arr, N);
}
}
|
Python3
def findLarger(arr, n):
mid = (n + 1 ) / / 2
mx = max (arr)
count = [ 0 ] * (mx + 1 )
for i in range (n):
count[arr[i]] + = 1
for i in range (mx, - 1 , - 1 ):
while (count[i] > 0 ):
count[i] - = 1
mid - = 1
print (i, end = " " )
if (mid = = 0 ):
break
if (mid = = 0 ):
break
if __name__ = = '__main__' :
arr = [ 10 , 4 , 2 , 8 , 9 ]
N = len (arr)
findLarger(arr, N)
|
C#
using System;
using System.Collections.Generic;
class GFG{
static void findLarger( int []arr, int n)
{
int mid = (n + 1) / 2;
int mx = Int32.MinValue;
for ( int i=0;i<n;i++){
if (arr[i]>mx)
mx = arr[i];
}
int []count = new int [mx + 1];
Array.Clear(count,0,mx+1);
for ( int i = 0; i < n; i++) {
count[arr[i]]++;
}
for ( int i = mx; i >= 0; i--) {
while (count[i] > 0) {
count[i]--;
mid--;
Console.Write(i+ " " );
if (mid == 0)
break ;
}
if (mid == 0)
break ;
}
}
public static void Main()
{
int []arr = { 10, 4, 2, 8, 9 };
int N = arr.Length;
findLarger(arr, N);
}
}
|
Javascript
<script>
function findLarger(arr, n) {
let mid = (n + 1) / 2;
let mx = Math.max.apply( null , arr)
var count = new Array(mx + 1).fill(0);
for (let i = 0; i < n; i++) {
count[arr[i]]++;
}
for (let i = mx; i >= 0; i--) {
while (count[i] > 0) {
count[i]--;
mid--;
document.write(i + ' ' );
if (mid == 0)
break ;
}
if (mid == 0)
break ;
}
}
var arr = [10, 4, 2, 8, 9];
var N = 5;
findLarger(arr, N);
</script>
|
Time Complexity: O(max(M2, N)), where M is the maximum element of the array arr[] and N is the length of the given array.
Auxiliary Space: O(M)
Share your thoughts in the comments
Please Login to comment...