📜  数组中没有元素更大的元素

📅  最后修改于: 2022-05-13 01:57:47.925000             🧑  作者: Mango

数组中没有元素更大的元素

给定一个整数数组,任务是找到所有元素都较小的元素的计数。始终计算第一个元素,因为它之前没有其他元素。

例子:

Input :   arr[] = {10, 40, 23, 35, 50, 7}
 Output :  3
The elements are 10, 40 and 50.

 Input :   arr[] = {5, 4, 1}
 Output :  1 

一种朴素的方法是逐个考虑一个元素并检查所有先前的元素。如果一个元素大于全部,则递增结果。

一种有效的方法是将最大值存储在数组中每个索引处,如果下一个元素大于最大值,则增加结果并使用该元素更新最大值。

下面是这个方法的实现。

C++
// C++ program to find elements that are greater than all
// previous elements
#include 
using namespace std;
 
// Function to count elements that are greater than all
// previous elements
int countElements(int arr[], int n)
{
    // First element will always be considered as greater
    // than previous ones
    int result = 1;
 
    // Store the arr[0] as maximum
    int max_ele = arr[0];
 
    // Traverse array starting from second element
    for (int i = 1; i < n; i++) {
        // Compare current element with the maximum
        // value if it is true otherwise continue
        if (arr[i] > max_ele) {
            // Update the maximum value
            max_ele = arr[i];
 
            // Increment the result
            result++;
        }
    }
 
    return result;
}
 
// Driver code
int main()
{
    int arr[] = { 10, 40, 23, 35, 50, 7 };
    int n = sizeof(arr) / sizeof(arr[0]);
    cout << countElements(arr, n);
    return 0;
}


Java
// Java program to find elements that are greater than all
// previous elements
 
class Test {
    // Method to count elements that are greater than all
    // previous elements
    static int countElements(int arr[], int n)
    {
        // First element will always be considered as greater
        // than previous ones
        int result = 1;
 
        // Store the arr[0] as maximum
        int max_ele = arr[0];
 
        // Traverse array starting from second element
        for (int i = 1; i < n; i++) {
            // Compare current element with the maximum
            // value if it is true otherwise continue
            if (arr[i] > max_ele) {
                // Update the maximum value
                max_ele = arr[i];
 
                // Increment the result
                result++;
            }
        }
 
        return result;
    }
 
    // Driver method
    public static void main(String[] args)
    {
        int arr[] = { 10, 40, 23, 35, 50, 7 };
        System.out.println(countElements(arr, arr.length));
    }
}


Python 3
# Python 3 program to find
# elements that are greater
# than all previous elements
 
# Function to count elements
# that are greater than all
# previous elements
def countElements(arr, n):
 
    # First element will always
    # be considered as greater
    # than previous ones
    result = 1
 
    # Store the arr[0]
    # as maximum
    max_ele = arr[0]
 
    # Traverse array starting
    # from second element
    for i in range(1, n ):
        # Compare current element
        # with the maximum
        # value if it is true
        # otherwise continue
        if (arr[i] > max_ele):
             
            # Update the
            # maximum value
            max_ele = arr[i]
 
            # Increment
            # the result
            result += 1
 
    return result
 
# Driver code
arr = [10, 40, 23,
       35, 50, 7]
n = len(arr)
print(countElements(arr, n))
 
# This code is contributed
# by Smitha


C#
// C# program to find elements that
// are greater than all previous elements
using System;
 
class GFG {
     
    // Method to count elements that are
    // greater than all previous elements
    static int countElements(int[] arr, int n)
    {
        // First element will always be considered
        // as greater than previous ones
        int result = 1;
 
        // Store the arr[0] as maximum
        int max_ele = arr[0];
 
        // Traverse array starting from second element
        for (int i = 1; i < n; i++) {
             
            // Compare current element with the maximum
            // value if it is true otherwise continue
            if (arr[i] > max_ele) {
                 
                // Update the maximum value
                max_ele = arr[i];
 
                // Increment the result
                result++;
            }
        }
 
        return result;
    }
 
    // Driver method
    public static void Main()
    {
        int[] arr = { 10, 40, 23, 35, 50, 7 };
        Console.WriteLine(countElements(arr, arr.Length));
    }
}
 
// This code is contributed by Sam007


PHP
 $max_ele)
        {
             
            // Update the maximum value
            $max_ele = $arr[$i];
 
            // Increment the result
            $result++;
        }
    }
 
    return $result;
}
 
    // Driver code
    $arr = array(10, 40, 23, 35, 50, 7);
    $n = sizeof($arr);
    echo countElements($arr, $n);
 
// This code is contributed by nitin mittal.
?>


Javascript


输出:

3

时间复杂度: O(n),其中 n 是输入中的元素数。
辅助空间: O(1)