📌  相关文章
📜  大于左边所有元素的数组元素的数量

📅  最后修改于: 2021-05-14 08:23:39             🧑  作者: Mango

给定一个大小为N的数组arr [] ,任务是计算数组元素的数量,以使它左边的所有元素都严格小于它。
注意:数组的第一个元素将被视为始终满足条件。

例子

方法:
请按照以下步骤解决问题:

  • 设置count = 1 ,因为第一个数组元素将被视为满足条件。
  • 将第一个数组元素(即arr [0] )设置为maximum
  • i = 1开始遍历数组,并将每个数组元素与当前的最大值进行比较。
  • 如果发现数组元素大于当前的最大值,则该元素满足条件,因为其左侧的所有元素都小于它。因此,增加count ,并更新最大值。
  • 最后,打印计数。

下面是上述方法的实现:

C++
// C++ program to implement the
//above approach
#include
using namespace std;
 
// Function to return the count
// of array elements with all
// elements to its left smaller
// than it
int count_elements(int arr[], int n)
{
     
    // Stores the count
    int count = 1;
 
    // Stores the maximum
    int max = arr[0];
 
    // Iterate over the array
    for(int i = 1; i < n; i++)
    {
         
        // If an element greater
        // than maximum is obtained
        if (arr[i] > max)
        {
             
            // Increase count
            count += 1;
 
            // Update maximum
            max = arr[i];
        }
    }
    return count;
}
 
// Driver Code
int main()
{
    int arr[] = { 2, 1, 4, 6, 3 };
    int n = sizeof(arr) / sizeof(arr[0]);
     
    cout << (count_elements(arr, n));
}
 
// This code is contributed by chitranayal


Java
// Java program to implement the
//above approach
import java.util.*;
 
class GFG{
     
// Function to return the count
// of array elements with all
// elements to its left smaller
// than it
static int count_elements(int arr[], int n)
{
     
    // Stores the count
    int count = 1;
 
    // Stores the maximum
    int max = arr[0];
 
    // Iterate over the array
    for(int i = 1; i < n; i++)
    {
         
        // If an element greater
        // than maximum is obtained
        if (arr[i] > max)
        {
             
            // Increase count
            count += 1;
 
            // Update maximum
            max = arr[i];
        }
    }
    return count;
}
 
// Driver Code
public static void main(String s[])
{
    int arr[] = { 2, 1, 4, 6, 3 };
    int n = arr.length;
     
    System.out.print(count_elements(arr, n));
}
}
 
// This code is contributed by rutvik_56


Python3
# Python3 Program to implement
# the above approach
 
# Function to return the count
# of array elements with all
# elements to its left smaller
# than it
 
 
def count_elements(arr):
 
    # Stores the count
    count = 1
 
    # Stores the maximum
    max = arr[0]
 
    # Iterate over the array
    for i in range(1, len(arr)):
 
        # If an element greater
        # than maximum is obtained
        if arr[i] > max:
 
            # Increase count
            count += 1
 
            # Update maximum
            max = arr[i]
    return count
 
 
# Driver Code
arr = [2, 1, 4, 6, 3]
print(count_elements(arr))


C#
// C# program to implement the
// above approach
using System;
 
class GFG{
 
// Function to return the count
// of array elements with all
// elements to its left smaller
// than it
static int count_elements(int[] arr, int n)
{
     
    // Stores the count
    int count = 1;
 
    // Stores the maximum
    int max = arr[0];
 
    // Iterate over the array
    for(int i = 1; i < n; i++)
    {
         
        // If an element greater
        // than maximum is obtained
        if (arr[i] > max)
        {
             
            // Increase count
            count += 1;
 
            // Update maximum
            max = arr[i];
        }
    }
    return count;
}
 
// Driver Code
public static void Main()
{
    int[] arr = { 2, 1, 4, 6, 3 };
    int n = arr.Length;
 
    Console.Write(count_elements(arr, n));
}
}
 
// This code is contributed by jrishabh99


Javascript


输出:
3

时间复杂度: O(N)
辅助空间: O(1)