Find elements larger than half of the elements in an array | Set 2
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:
- Initialize a variable, say mid as (N + 1)/2 to store the middle index of the array.
- Initialize a variable, say max to store the maximum element of the array.
- Initialize an array count[] to store the frequency of each element.
- Traverse the array, arr[] and increment count[arr[i]] by 1.
- Traverse the array count[] using the variable i from the back and perform the following steps:
- Iterate until the value of the count[i] is positive and perform the following steps:
- Print the value of i and decrease the value of count[i] and mid by 1.
- If the value of mid is 0, then break out of the loop.
- If the value of mid is 0, then break out of the loop.
- Iterate until the value of the count[i] is positive and perform the following steps:
Below is the implementation of the above approach:
C++
// C++ program for the above approach #include <bits/stdc++.h> using namespace std; // Function to find the element that // are larger than half of elements // of the array void findLarger( int arr[], int n) { // Find the value of mid int mid = (n + 1) / 2; // Stores the maximum element int mx = *max_element(arr, arr + n); // Stores the frequency of each // array element int count[mx + 1] = { 0 }; for ( int i = 0; i < n; i++) { count[arr[i]]++; } // Traverse the array in the // reverse order for ( int i = mx; i >= 0; i--) { while (count[i] > 0) { // Decrement the value // of count[i] and mid count[i]--; mid--; // Print the current element cout << i << ' ' ; // Check if the value of // mid is equal to 0 if (mid == 0) break ; } if (mid == 0) break ; } } // Driver Code int main() { int arr[] = { 10, 4, 2, 8, 9 }; int N = sizeof (arr) / sizeof (arr[0]); findLarger(arr, N); return 0; } |
Java
// Java program for the above approach import java.util.*; class GFG{ // Function to find the element that // are larger than half of elements // of the array static void findLarger( int arr[], int n) { // Find the value of mid int mid = (n + 1 ) / 2 ; // Stores the maximum element int mx = Arrays.stream(arr).max().getAsInt(); // Stores the frequency of each // array element 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]]++; } // Traverse the array in the // reverse order for ( int i = mx; i >= 0 ; i--) { while (count[i] > 0 ) { // Decrement the value // of count[i] and mid count[i]--; mid--; // Print the current element System.out.print(i + " " ); // Check if the value of // mid is equal to 0 if (mid == 0 ) break ; } if (mid == 0 ) break ; } } // Driver Code public static void main(String[] args) { int arr[] = { 10 , 4 , 2 , 8 , 9 }; int N = arr.length; findLarger(arr, N); } } // This code is contributed by sanjoy_62. |
Python3
# Python program for the above approach # Function to find the element that # are larger than half of elements # of the array def findLarger(arr, n): # Find the value of mid mid = (n + 1 ) / / 2 # Stores the maximum element mx = max (arr) # Stores the frequency of each # array element count = [ 0 ] * (mx + 1 ) for i in range (n): count[arr[i]] + = 1 # Traverse the array in the # reverse order for i in range (mx, - 1 , - 1 ): while (count[i] > 0 ): # Decrement the value # of count[i] and mid count[i] - = 1 mid - = 1 # Print current element print (i, end = " " ) # Check if the value of # mid is equal to 0 if (mid = = 0 ): break if (mid = = 0 ): break # Driver Code if __name__ = = '__main__' : arr = [ 10 , 4 , 2 , 8 , 9 ] N = len (arr) findLarger(arr, N) # This code is contributed by mohit kumar 29. |
C#
// C# program for the above approach using System; using System.Collections.Generic; class GFG{ // Function to find the element that // are larger than half of elements // of the array static void findLarger( int []arr, int n) { // Find the value of mid int mid = (n + 1) / 2; // Stores the maximum element int mx = Int32.MinValue; for ( int i=0;i<n;i++){ if (arr[i]>mx) mx = arr[i]; } // Stores the frequency of each // array element int []count = new int [mx + 1]; Array.Clear(count,0,mx+1); for ( int i = 0; i < n; i++) { count[arr[i]]++; } // Traverse the array in the // reverse order for ( int i = mx; i >= 0; i--) { while (count[i] > 0) { // Decrement the value // of count[i] and mid count[i]--; mid--; // Print the current element Console.Write(i+ " " ); // Check if the value of // mid is equal to 0 if (mid == 0) break ; } if (mid == 0) break ; } } // Driver Code public static void Main() { int []arr = { 10, 4, 2, 8, 9 }; int N = arr.Length; findLarger(arr, N); } } // This code is contributed by SURENDRA_GANGWAR. |
Javascript
<script> // JavaScript program for the above approach // Function to find the element that // are larger than half of elements // of the array function findLarger(arr, n) { // Find the value of mid let mid = (n + 1) / 2; // Stores the maximum element let mx = Math.max.apply( null , arr) // Stores the frequency of each // array element var count = new Array(mx + 1).fill(0); for (let i = 0; i < n; i++) { count[arr[i]]++; } // Traverse the array in the // reverse order for (let i = mx; i >= 0; i--) { while (count[i] > 0) { // Decrement the value // of count[i] and mid count[i]--; mid--; // Print the current element document.write(i + ' ' ); // Check if the value of // mid is equal to 0 if (mid == 0) break ; } if (mid == 0) break ; } } // Driver Code var arr = [10, 4, 2, 8, 9]; var N = 5; findLarger(arr, N); </script> |
10 9 8
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)
Please Login to comment...