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++ code for the approach #include <bits/stdc++.h> using namespace std;
// Function to find the elements that are greater // than half of elements in an array void greaterThanHalf( int arr[], int n) {
// traversing each element of array
for ( int i = 0; i < n; i++) {
int count = 0;
// again traversing each element to
// count from how many numbers in the
// array arr[i] is greater
for ( int j = 0; j < n; j++) {
if (arr[j] < arr[i])
count++;
}
// if count is greater than n/2
// this is the element so print it
if (count >= n / 2)
cout << arr[i] << " " ;
}
} // Driver's code int main() {
// Input array
int arr[] = { 1, 3, 6, 1, 0, 9 };
int n = sizeof (arr) / sizeof (arr[0]);
// Function Call
greaterThanHalf(arr, n);
return 0;
} |
public class Main {
// Function to find the elements that are greater
// than half of elements in an array
static void greaterThanHalf( int arr[], int n) {
// traversing each element of array
for ( int i = 0 ; i < n; i++) {
int count = 0 ;
// again traversing each element to
// count from how many numbers in the
// array arr[i] is greater
for ( int j = 0 ; j < n; j++) {
if (arr[j] < arr[i])
count++;
}
// if count is greater than n/2
// this is the element so print it
if (count >= n / 2 )
System.out.print(arr[i] + " " );
}
}
// Driver's code
public static void main(String[] args) {
// Input array
int arr[] = { 1 , 3 , 6 , 1 , 0 , 9 };
int n = arr.length;
// Function Call
greaterThanHalf(arr, n);
}
} |
# Function to find the elements that are greater # than half of elements in an array def greaterThanHalf(arr, n):
# traversing each element of array
for i in range (n):
count = 0
# again traversing each element to
# count from how many numbers in the
# array arr[i] is greater
for j in range (n):
if (arr[j] < arr[i]):
count + = 1
# if count is greater than n/2
# this is the element so print it
if (count > = n / 2 ):
print (arr[i], end = " " )
# Driver's code arr = [ 1 , 3 , 6 , 1 , 0 , 9 ]
n = len (arr)
greaterThanHalf(arr, n) |
using System;
class Program
{ // Function to find the elements that are greater
// than half of elements in an array
static void GreaterThanHalf( int [] arr, int n)
{
// Traversing each element of the array
for ( int i = 0; i < n; i++)
{
int count = 0;
// Again traversing each element to
// count how many numbers in the array
// arr[i] is greater
for ( int j = 0; j < n; j++)
{
if (arr[j] < arr[i])
count++;
}
// If count is greater than or equal to n/2
// this is the element, so print it
if (count >= n / 2)
Console.Write(arr[i] + " " );
}
}
// Driver's code
static void Main( string [] args)
{
// Input array
int [] arr = { 1, 3, 6, 1, 0, 9 };
int n = arr.Length;
// Function Call
GreaterThanHalf(arr, n);
}
} // This code is contributed by shivamgupta0987654321 |
// Javascript code for the approach // Function to find the elements that are greater // than half of elements in an array function greaterThanHalf(arr, n) {
// traversing each element of array
for (let i = 0; i < n; i++) {
let count = 0;
// again traversing each element to
// count from how many numbers in the
// array arr[i] is greater
for (let j = 0; j < n; j++) {
if (arr[j] < arr[i])
count++;
}
// if count is greater than n/2
// this is the element so print it
if (count >= Math.floor(n / 2))
console.log(arr[i] + " " );
}
} // Driver's code // Input array let arr = [1, 3, 6, 1, 0, 9]; let n = arr.length; // Function Call greaterThanHalf(arr, n); |
3 6 9
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++ program to find elements that are larger than // half of the elements in array #include <bits/stdc++.h> using namespace std;
// Prints elements larger than n/2 element void findLarger( int arr[], int n)
{ // Sort the array in ascending order
sort(arr, arr + n);
// Print last ceil(n/2) elements
for ( int i = n-1; i >= n/2; i--)
cout << arr[i] << " " ;
} // Driver program int main()
{ int arr[] = {1, 3, 6, 1, 0, 9};
int n = sizeof (arr)/ sizeof (arr[0]);
findLarger(arr, n);
return 0;
} |
// Java program to find elements that are // larger than half of the elements in array import java.util.*;
class Gfg
{ // Prints elements larger than n/2 element
static void findLarger( int arr[], int n)
{
// Sort the array in ascending order
Arrays.sort(arr);
// Print last ceil(n/2) elements
for ( int i = n- 1 ; i >= n/ 2 ; i--)
System.out.print(arr[i] + " " );
}
// Driver program
public static void main(String[] args)
{
int arr[] = { 1 , 3 , 6 , 1 , 0 , 9 };
int n = arr.length;
findLarger(arr, n);
}
} // This code is contributed by Raghav Sharma |
# Python program to find elements that are larger than # half of the elements in array # Prints elements larger than n/2 element def findLarger(arr,n):
# Sort the array in ascending order
x = sorted (arr)
# Print last ceil(n/2) elements
for i in range (n / / 2 ,n):
print (x[i],end = " " )
# Driver program arr = [ 1 , 3 , 6 , 1 , 0 , 9 ]
n = len (arr);
findLarger(arr,n) # This code is contributed by Afzal Ansari |
// C# program to find elements // that are larger than half // of the elements in array using System;
class GFG
{ // Prints elements larger
// than n/2 element
static void findLarger( int []arr,
int n)
{
// Sort the array
// in ascending order
Array.Sort(arr);
// Print last ceil(n/2) elements
for ( int i = n - 1; i >= n / 2; i--)
Console.Write(arr[i] + " " );
}
// Driver Code
public static void Main()
{
int []arr = {1, 3, 6, 1, 0, 9};
int n = arr.Length;
findLarger(arr, n);
}
} // This code is contributed // by nitin mittal. |
<script> // JavaScript program to find elements that are // larger than half of the elements in array // Prints elements larger than n/2 element function findLarger(arr, n)
{ // Sort the array in ascending order
arr.sort();
// Print last ceil(n/2) elements
for (let i = n - 1; i >= n / 2; i--)
document.write(arr[i] + " " );
} // Driver Code let arr = [1, 3, 6, 1, 0, 9]; let n = arr.length; findLarger(arr, n); // This code is contributed by chinmoy1997pal </script> |
<?php // PHP program to find elements // that are larger than half of // the elements in array // Prints elements larger // than n/2 element function findLarger( $arr , $n )
{ // Sort the array in
// ascending order
sort( $arr );
// Print last ceil(n/2) elements
for ( $i = $n - 1; $i >= $n / 2; $i --)
echo $arr [ $i ] , " " ;
} // Driver Code $arr = array (1, 3, 6, 1, 0, 9);
$n = count ( $arr );
findLarger( $arr , $n );
// This code is contributed by anuj_67. ?> |
9 6 3
Time Complexity: O(n*log(n))
Auxiliary Space: O(1)
This article is contributed by Sahil Chhabra .