📌  相关文章
📜  查找数组中至少有两个大元素的所有元素

📅  最后修改于: 2021-04-23 07:49:58             🧑  作者: Mango

给定一个由n个不同元素组成的数组,任务是查找数组中所有比其至少多两个元素的元素。

例子 :

方法1(简单)
天真的方法是运行两个循环并逐一检查数组元素,以检查数组元素至少有两个元素大于或小于其自身。如果为true,则打印数组元素。

C++
// Simple C++ program to find
// all elements in array which
// have at-least two greater
// elements itself.
#include
using namespace std;
 
void findElements(int arr[], int n)
{
    // Pick elements one by one and
    // count greater elements. If
    // count is more than 2, print
    // that element.
    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 >= 2)
            cout << arr[i] << " ";
    }
}
 
// Driver code
int main()
{
    int arr[] = { 2, -6 ,3 , 5, 1};
    int n = sizeof(arr) / sizeof(arr[0]);
    findElements(arr, n);
    return 0;
}


Java
// Java program to find all
// elements in array which
// have at-least two greater
// elements itself.
import java.util.*;
import java.io.*;
 
class GFG
{
     
static void findElements(int arr[],
                            int n)
{
    // Pick elements one by one
    // and count greater elements.
    // If count is more than 2,
    // print that element.
    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 >= 2)
        System.out.print(arr[i] + " ");
    }
}
 
// Driver code
public static void main(String args[])
{
    int arr[] = { 2, -6 ,3 , 5, 1};
    int n = arr.length;
    findElements(arr, n);
}
}
 
// This code is contributed by Sahil_Bansall


Python3
# Python3 program to find
# all elements in array
# which have at-least two
# greater elements itself.
 
def findElements( arr, n):
 
    # Pick elements one by
        # one and count greater
    # elements. If count
        # is more than 2, print
    # that element.
 
    for i in range(n):
        count = 0
        for j in range(0, n):
            if arr[j] > arr[i]:
                count = count + 1
                         
                 
        if count >= 2 :
            print(arr[i], end=" ")
             
 
# Driver code
arr = [ 2, -6 ,3 , 5, 1]
n = len(arr)
findElements(arr, n)
     
# This code is contributed by sunnysingh


C#
// C# program to find all elements in
// array which have at least two greater
// elements itself.
using System;
 
class GFG
{
     
static void findElements(int []arr, int n)
{
    // Pick elements one by one and count
    // greater elements. If count is more
    // than 2, print that element.
    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 >= 2)
    Console.Write(arr[i] + " ");
    }
}
 
// Driver code
public static void Main(String []args)
{
    int []arr = {2, -6 ,3 , 5, 1};
    int n = arr.Length;
    findElements(arr, n);
 
}
}
 
// This code is contributed by Parashar.


PHP
 $arr[$i])
                $count++;
 
        if ($count >= 2)
            echo $arr[$i]." ";
    }
}
 
// Driver code
$arr = array( 2, -6 ,3 , 5, 1);
$n = sizeof($arr);
findElements($arr, $n);
 
?>


Javascript


C++
// Sorting based C++ program to
// find all elements in array
// which have atleast two greater
// elements itself.
#include
using namespace std;
 
void findElements(int arr[], int n)
{
    sort(arr, arr + n);
 
    for (int i = 0; i < n - 2; i++)
    cout << arr[i] << " ";
}
 
// Driver Code
int main()
{
    int arr[] = { 2, -6 ,3 , 5, 1};
    int n = sizeof(arr) / sizeof(arr[0]);
    findElements(arr, n);
    return 0;
}


Java
// Sorting based Java program to find
// all elements in array which have
// atleast two greater elements itself.
import java.util.*;
import java.io.*;
 
class GFG
{
 
static void findElements(int arr[], int n)
{
    Arrays.sort(arr);
 
    for (int i = 0; i < n - 2; i++)
    System.out.print(arr[i] + " ");
}
 
// Driver code
public static void main(String args[])
{
    int arr[] = { 2, -6 ,3 , 5, 1};
    int n = arr.length;
    findElements(arr, n);
 
}
}
 
// This code is contributed by Sahil_Bansall


Python3
# Sorting based Python 3 program
# to find all elements in array
# which have atleast two greater
# elements itself.
 
def findElements(arr, n):
 
    arr.sort()
 
    for i in range(0, n-2):
        print(arr[i], end =" ")
 
# Driven source
arr = [2, -6, 3, 5, 1]
n = len(arr)
findElements(arr, n)
 
# This code is contributed
# by Smitha Dinesh Semwal


C#
// Sorting based C# program to find
// all elements in array which have
// atleast two greater elements itself.
using System;
 
class GFG
{
 
static void findElements(int []arr, int n)
{
    Array.Sort(arr);
 
    for (int i = 0; i < n-2; i++)
        Console.Write(arr[i] + " ");
}
 
// Driver code
public static void Main(String []args)
{
    int []arr = { 2, -6 ,3 , 5, 1};
    int n = arr.Length;
    findElements(arr, n);
 
}
}
 
// This code is contributed by parashar


PHP
;


Javascript


C++
// C++ program to find all elements
// in array which have atleast two
// greater elements itself.
#include
using namespace std;
 
void findElements(int arr[], int n)
{
    int first = INT_MIN,
        second = INT_MIN;
    for (int i = 0; i < n; i++)
    {
        /* If current element is smaller
        than first then update both first
        and second */
        if (arr[i] > first)
        {
            second = first;
            first = arr[i];
        }
 
        /* If arr[i] is in between first
        and second then update second */
        else if (arr[i] > second)
            second = arr[i];
    }
 
    for (int i = 0; i < n; i++)
        if (arr[i] < second)
            cout << arr[i] << " ";
}
 
// Driver code
int main()
{
    int arr[] = { 2, -6, 3, 5, 1};
    int n = sizeof(arr) / sizeof(arr[0]);
    findElements(arr, n);
    return 0;
}


Java
// Java program to find all elements
// in array which have atleast
// two greater elements itself.
import java.util.*;
import java.io.*;
 
class GFG
{
     
static void findElements(int arr[], int n)
{
    int first = Integer.MIN_VALUE;
    int second = Integer.MAX_VALUE;
     
    for (int i = 0; i < n; i++)
    {
        // If current element is smaller
        // than first then update both
        // first and second
        if (arr[i] > first)
        {
            second = first;
            first = arr[i];
        }
 
        /* If arr[i] is in between
        first and second
        then update second */
        else if (arr[i] > second)
            second = arr[i];
    }
 
    for (int i = 0; i < n; i++)
        if (arr[i] < second)
            System.out.print(arr[i] + " ") ;
}
// Driver code
public static void main(String args[])
{
    int arr[] = { 2, -6, 3, 5, 1};
    int n = arr.length;
    findElements(arr, n);
}
}
 
// This code is contributed by Sahil_Bansall


Python3
# Python3 program to find all elements
# in array which have atleast two
# greater elements itself.
import sys
 
def findElements(arr, n):
 
    first = -sys.maxsize
    second = -sys.maxsize
 
    for i in range(0, n):
     
        # If current element is smaller
        # than first then update both
        # first and second
        if (arr[i] > first):
         
            second = first
            first = arr[i]
         
        # If arr[i] is in between first
        # and second then update second
        elif (arr[i] > second):
            second = arr[i]
     
    for i in range(0, n):
        if (arr[i] < second):
            print(arr[i], end =" ")
 
 
# Driver code
arr = [2, -6, 3, 5, 1]
n = len(arr)
findElements(arr, n)
 
# This code is contributed
# by Smitha Dinesh Semwal


C#
// C# program to find all elements
// in array which have atleast
// two greater elements itself.
using System;
 
class GFG
{
    static void findElements(int []arr,
                            int n)
    {
    int first = int.MinValue;
    int second = int.MaxValue;
     
    for (int i = 0; i < n; i++)
    {
        // If current element is smaller
        // than first then update both
        // first and second
        if (arr[i] > first)
        {
            second = first;
            first = arr[i];
        }
 
        /* If arr[i] is in between
        first and second
        then update second */
        else if (arr[i] > second)
            second = arr[i];
    }
 
    for (int i = 0; i < n; i++)
        if (arr[i] < second)
            Console.Write(arr[i] + " ") ;
}
// Driver code
public static void Main(String []args)
{
    int []arr = { 2, -6, 3, 5, 1};
    int n = arr.Length;
    findElements(arr, n);
}
}
 
// This code is contributed by parashar...


PHP
 $first)
            {
                $second = $first;
                $first = $arr[$i];
             
     
            }
 
        /* If arr[i] is in between first
           and second then update second */
        else if ($arr[$i] > $second)
            $second = $arr[$i];
    }
 
    for($i = 0; $i < $n; $i++)
        if ($arr[$i] < $second)
            echo $arr[$i] , " ";
}
 
    // Driver code
    $arr = array(2, -6, 3, 5, 1);
    $n = count($arr);
    findElements($arr, $n);
 
// This code is contributed by vishal tripathi.
?>


输出
2 -6 1 

时间复杂度: O(n 2 )

方法2(使用排序)
我们先按升序对数组进行排序,然后再打印n-2个元素,其中n是数组的大小。

C++

// Sorting based C++ program to
// find all elements in array
// which have atleast two greater
// elements itself.
#include
using namespace std;
 
void findElements(int arr[], int n)
{
    sort(arr, arr + n);
 
    for (int i = 0; i < n - 2; i++)
    cout << arr[i] << " ";
}
 
// Driver Code
int main()
{
    int arr[] = { 2, -6 ,3 , 5, 1};
    int n = sizeof(arr) / sizeof(arr[0]);
    findElements(arr, n);
    return 0;
}

Java

// Sorting based Java program to find
// all elements in array which have
// atleast two greater elements itself.
import java.util.*;
import java.io.*;
 
class GFG
{
 
static void findElements(int arr[], int n)
{
    Arrays.sort(arr);
 
    for (int i = 0; i < n - 2; i++)
    System.out.print(arr[i] + " ");
}
 
// Driver code
public static void main(String args[])
{
    int arr[] = { 2, -6 ,3 , 5, 1};
    int n = arr.length;
    findElements(arr, n);
 
}
}
 
// This code is contributed by Sahil_Bansall

Python3

# Sorting based Python 3 program
# to find all elements in array
# which have atleast two greater
# elements itself.
 
def findElements(arr, n):
 
    arr.sort()
 
    for i in range(0, n-2):
        print(arr[i], end =" ")
 
# Driven source
arr = [2, -6, 3, 5, 1]
n = len(arr)
findElements(arr, n)
 
# This code is contributed
# by Smitha Dinesh Semwal

C#

// Sorting based C# program to find
// all elements in array which have
// atleast two greater elements itself.
using System;
 
class GFG
{
 
static void findElements(int []arr, int n)
{
    Array.Sort(arr);
 
    for (int i = 0; i < n-2; i++)
        Console.Write(arr[i] + " ");
}
 
// Driver code
public static void Main(String []args)
{
    int []arr = { 2, -6 ,3 , 5, 1};
    int n = arr.Length;
    findElements(arr, n);
 
}
}
 
// This code is contributed by parashar

的PHP

;

Java脚本


输出
-6 1 2 

时间复杂度: O(n Log n)

方法3(高效)
在第二种方法中,我们只需计算数组的第二个最大值元素,然后打印所有小于或等于第二个最大值的元素。

C++

// C++ program to find all elements
// in array which have atleast two
// greater elements itself.
#include
using namespace std;
 
void findElements(int arr[], int n)
{
    int first = INT_MIN,
        second = INT_MIN;
    for (int i = 0; i < n; i++)
    {
        /* If current element is smaller
        than first then update both first
        and second */
        if (arr[i] > first)
        {
            second = first;
            first = arr[i];
        }
 
        /* If arr[i] is in between first
        and second then update second */
        else if (arr[i] > second)
            second = arr[i];
    }
 
    for (int i = 0; i < n; i++)
        if (arr[i] < second)
            cout << arr[i] << " ";
}
 
// Driver code
int main()
{
    int arr[] = { 2, -6, 3, 5, 1};
    int n = sizeof(arr) / sizeof(arr[0]);
    findElements(arr, n);
    return 0;
}

Java

// Java program to find all elements
// in array which have atleast
// two greater elements itself.
import java.util.*;
import java.io.*;
 
class GFG
{
     
static void findElements(int arr[], int n)
{
    int first = Integer.MIN_VALUE;
    int second = Integer.MAX_VALUE;
     
    for (int i = 0; i < n; i++)
    {
        // If current element is smaller
        // than first then update both
        // first and second
        if (arr[i] > first)
        {
            second = first;
            first = arr[i];
        }
 
        /* If arr[i] is in between
        first and second
        then update second */
        else if (arr[i] > second)
            second = arr[i];
    }
 
    for (int i = 0; i < n; i++)
        if (arr[i] < second)
            System.out.print(arr[i] + " ") ;
}
// Driver code
public static void main(String args[])
{
    int arr[] = { 2, -6, 3, 5, 1};
    int n = arr.length;
    findElements(arr, n);
}
}
 
// This code is contributed by Sahil_Bansall

Python3

# Python3 program to find all elements
# in array which have atleast two
# greater elements itself.
import sys
 
def findElements(arr, n):
 
    first = -sys.maxsize
    second = -sys.maxsize
 
    for i in range(0, n):
     
        # If current element is smaller
        # than first then update both
        # first and second
        if (arr[i] > first):
         
            second = first
            first = arr[i]
         
        # If arr[i] is in between first
        # and second then update second
        elif (arr[i] > second):
            second = arr[i]
     
    for i in range(0, n):
        if (arr[i] < second):
            print(arr[i], end =" ")
 
 
# Driver code
arr = [2, -6, 3, 5, 1]
n = len(arr)
findElements(arr, n)
 
# This code is contributed
# by Smitha Dinesh Semwal

C#

// C# program to find all elements
// in array which have atleast
// two greater elements itself.
using System;
 
class GFG
{
    static void findElements(int []arr,
                            int n)
    {
    int first = int.MinValue;
    int second = int.MaxValue;
     
    for (int i = 0; i < n; i++)
    {
        // If current element is smaller
        // than first then update both
        // first and second
        if (arr[i] > first)
        {
            second = first;
            first = arr[i];
        }
 
        /* If arr[i] is in between
        first and second
        then update second */
        else if (arr[i] > second)
            second = arr[i];
    }
 
    for (int i = 0; i < n; i++)
        if (arr[i] < second)
            Console.Write(arr[i] + " ") ;
}
// Driver code
public static void Main(String []args)
{
    int []arr = { 2, -6, 3, 5, 1};
    int n = arr.Length;
    findElements(arr, n);
}
}
 
// This code is contributed by parashar...

的PHP

 $first)
            {
                $second = $first;
                $first = $arr[$i];
             
     
            }
 
        /* If arr[i] is in between first
           and second then update second */
        else if ($arr[$i] > $second)
            $second = $arr[$i];
    }
 
    for($i = 0; $i < $n; $i++)
        if ($arr[$i] < $second)
            echo $arr[$i] , " ";
}
 
    // Driver code
    $arr = array(2, -6, 3, 5, 1);
    $n = count($arr);
    findElements($arr, $n);
 
// This code is contributed by vishal tripathi.
?>
输出
2 -6 1 

时间复杂度: O(n)