Given an array of n elements, the task is to find the elements that are greater than half of elements in an array. In case of odd elements, we need to print elements larger than floor(n/2) elements where n is total number of elements in array.
Examples :
Input : arr[] = {1, 6, 3, 4}
Output : 4 6
Input : arr[] = {10, 4, 2, 8, 9}
Output : 10 9 8
A naive approach is to take an element and compare it with all other elements and if it is greater, then increment the count and then check if count is greater than n/2 elements, then print.
Algorithm:
- Declare a function named greaterThanHalf that takes an integer array arr and an integer n as inputs.
- Traverse through each element of the array from index i = 0 to i = n-1.
- Declare a variable count and initialize it to 0.
- Traverse through each element of the array from index j = 0 to j = n-1.
- If the element at index j is less than the element at index i, increment count by 1.
- If count is greater than or equal to n/2, print the element at index i.
Below is the implementation of the approach:
C++
#include <bits/stdc++.h>
using namespace std;
void greaterThanHalf( int arr[], int n) {
for ( int i = 0; i < n; i++) {
int count = 0;
for ( int j = 0; j < n; j++) {
if (arr[j] < arr[i])
count++;
}
if (count >= n / 2)
cout << arr[i] << " " ;
}
}
int main() {
int arr[] = { 1, 3, 6, 1, 0, 9 };
int n = sizeof (arr) / sizeof (arr[0]);
greaterThanHalf(arr, n);
return 0;
}
|
Java
public class Main {
static void greaterThanHalf( int arr[], int n) {
for ( int i = 0 ; i < n; i++) {
int count = 0 ;
for ( int j = 0 ; j < n; j++) {
if (arr[j] < arr[i])
count++;
}
if (count >= n / 2 )
System.out.print(arr[i] + " " );
}
}
public static void main(String[] args) {
int arr[] = { 1 , 3 , 6 , 1 , 0 , 9 };
int n = arr.length;
greaterThanHalf(arr, n);
}
}
|
Python3
def greaterThanHalf(arr, n):
for i in range (n):
count = 0
for j in range (n):
if (arr[j] < arr[i]):
count + = 1
if (count > = n / 2 ):
print (arr[i], end = " " )
arr = [ 1 , 3 , 6 , 1 , 0 , 9 ]
n = len (arr)
greaterThanHalf(arr, n)
|
C#
using System;
class Program
{
static void GreaterThanHalf( int [] arr, int n)
{
for ( int i = 0; i < n; i++)
{
int count = 0;
for ( int j = 0; j < n; j++)
{
if (arr[j] < arr[i])
count++;
}
if (count >= n / 2)
Console.Write(arr[i] + " " );
}
}
static void Main( string [] args)
{
int [] arr = { 1, 3, 6, 1, 0, 9 };
int n = arr.Length;
GreaterThanHalf(arr, n);
}
}
|
Javascript
function greaterThanHalf(arr, n) {
for (let i = 0; i < n; i++) {
let count = 0;
for (let j = 0; j < n; j++) {
if (arr[j] < arr[i])
count++;
}
if (count >= Math.floor(n / 2))
console.log(arr[i] + " " );
}
}
let arr = [1, 3, 6, 1, 0, 9];
let n = arr.length;
greaterThanHalf(arr, n);
|
Time Complexity: O(N*N) as two nested loops are executing both from 1 to N where N is size of the input array.
Space Complexity: O(1) as no extra space has been used.
An efficient method is to sort the array in ascending order and then print last ceil(n/2) elements from sorted array.
Below is the implementation of this sorting based approach.
Implementation:
C++
#include <bits/stdc++.h>
using namespace std;
void findLarger( int arr[], int n)
{
sort(arr, arr + n);
for ( int i = n-1; i >= n/2; i--)
cout << arr[i] << " " ;
}
int main()
{
int arr[] = {1, 3, 6, 1, 0, 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)
{
Arrays.sort(arr);
for ( int i = n- 1 ; i >= n/ 2 ; i--)
System.out.print(arr[i] + " " );
}
public static void main(String[] args)
{
int arr[] = { 1 , 3 , 6 , 1 , 0 , 9 };
int n = arr.length;
findLarger(arr, n);
}
}
|
Python3
def findLarger(arr,n):
x = sorted (arr)
for i in range (n / / 2 ,n):
print (x[i],end = " " )
arr = [ 1 , 3 , 6 , 1 , 0 , 9 ]
n = len (arr);
findLarger(arr,n)
|
C#
using System;
class GFG
{
static void findLarger( int []arr,
int n)
{
Array.Sort(arr);
for ( int i = n - 1; i >= n / 2; i--)
Console.Write(arr[i] + " " );
}
public static void Main()
{
int []arr = {1, 3, 6, 1, 0, 9};
int n = arr.Length;
findLarger(arr, n);
}
}
|
Javascript
<script>
function findLarger(arr, n)
{
arr.sort();
for (let i = n - 1; i >= n / 2; i--)
document.write(arr[i] + " " );
}
let arr = [1, 3, 6, 1, 0, 9];
let n = arr.length;
findLarger(arr, n);
</script>
|
PHP
<?php
function findLarger( $arr , $n )
{
sort( $arr );
for ( $i = $n - 1; $i >= $n / 2; $i --)
echo $arr [ $i ] , " " ;
}
$arr = array (1, 3, 6, 1, 0, 9);
$n = count ( $arr );
findLarger( $arr , $n );
?>
|
Time Complexity: O(n*log(n))
Auxiliary Space: O(1)
This article is contributed by Sahil Chhabra . If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!
Last Updated :
30 Oct, 2023
Like Article
Save Article