查找大于数组中元素一半的元素

给定一个由n个元素组成的数组,任务是找到数组中超过一半元素的元素。对于奇数元素,我们需要打印大于地板(n/2)元素的元素,其中n是数组中元素的总数。

null

例如:

Input :  arr[] = {1, 6, 3, 4}Output : 4 6Input  :  arr[] = {10, 4, 2, 8, 9}Output :  10 9 8

A. 幼稚的方法 取一个元素与所有其他元素进行比较,如果大于,则增加计数,然后检查计数是否大于n/2个元素,然后打印。

有效方法 是按升序对数组进行排序,然后打印排序数组中的最后一个ceil(n/2)元素。 下面是这种基于排序的方法的实现。

C++

// 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

// 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


Python3

# 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#

// 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.


PHP

<?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.
?>


Javascript

<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>


输出:

9 6 3

本文由 萨希尔·查布拉(杀手) .如果你喜欢GeekSforgek,并想贡献自己的力量,你也可以使用 写极客。组织 或者把你的文章寄去评论-team@geeksforgeeks.org.看到你的文章出现在Geeksforgeks主页上,并帮助其他极客。 如果您发现任何不正确的地方,或者您想分享有关上述主题的更多信息,请写下评论。

© 版权声明
THE END
喜欢就支持一下吧
点赞5 分享