📜  计算位置,使之前的所有元素都更大

📅  最后修改于: 2021-05-07 08:30:01             🧑  作者: Mango

给定一个数组A [] ,任务是找到数组中位置i的数量,以使A [i]之前的所有元素都大于A [i]。

注意:因为第一个元素之前没有其他元素,所以始终将其计算在内。

例子:

Input: N = 4, A[] = {2, 1, 3, 5} 
Output: 2
The valid positions are 1, 2.

Input : N = 3, A[] = {7, 6, 5}
Output: 3
All three positions are valid positions.

这个想法是在遍历数组时每次计算最小元素。那是:

  • 将第一个元素初始化为最小元素。
  • 每次有新元素到达时,检查这是否是新的最小值,如果是,则增加有效位置的数量,并将最小值初始化为新的最小值。

下面是上述方法的实现:

CPP
// C++ Program to count positions such that all
// elements before it are greater
  
#include 
using namespace std;
  
// Function to count positions such that all
// elements before it are greater
int getPositionCount(int a[], int n)
{   
    // Count is initially 1 for the first element
    int count = 1;
      
    // Inital Minimum
    int min = a[0];
      
    // Traverse the array
    for(int i=1; i


Java
// Java Program to count positions such that all
// elements before it are greater
class GFG
{
  
// Function to count positions such that all
// elements before it are greater
static int getPositionCount(int a[], int n)
{ 
    // Count is initially 1 for the first element
    int count = 1;
      
    // Inital Minimum
    int min = a[0];
      
    // Traverse the array
    for(int i = 1; i < n; i++)
    { 
        // If current element is new minimum
        if(a[i] <= min)
        {
            // Update minimum
            min = a[i];
              
            // Increment count
            count++;
        }
    }
      
    return count;
}
  
// Driver Code
public static void main(String[] args)
{
    int a[] = { 5, 4, 6, 1, 3, 1 };
    int n = a.length;
  
    System.out.print(getPositionCount(a, n));
}
}
  
// This code is contributed by PrinciRaj1992


Python3
# Python3 Program to count positions such that all 
# elements before it are greater 
  
# Function to count positions such that all 
# elements before it are greater 
def getPositionCount(a, n) :
  
    # Count is initially 1 for the first element 
    count = 1; 
      
    # Inital Minimum 
    min = a[0]; 
      
    # Traverse the array 
    for i in range(1, n) :
      
        # If current element is new minimum 
        if(a[i] <= min) :
          
            # Update minimum 
            min = a[i]; 
              
            # Increment count 
            count += 1; 
      
    return count; 
  
# Driver Code 
if __name__ == "__main__" : 
  
    a = [ 5, 4, 6, 1, 3, 1 ]; 
    n = len(a);
      
    print(getPositionCount(a, n)); 
      
# This code is contributed by AnkitRai01


C#
// C# Program to count positions such that all
// elements before it are greater
using System;
  
class GFG
{
      
    // Function to count positions such that all
    // elements before it are greater
    static int getPositionCount(int []a, int n)
    { 
        // Count is initially 1 for the first element
        int count = 1;
          
        // Inital Minimum
        int min = a[0];
          
        // Traverse the array
        for(int i = 1; i < n; i++)
        { 
            // If current element is new minimum
            if(a[i] <= min)
            {
                // Update minimum
                min = a[i];
                  
                // Increment count
                count++;
            }
        }
          
        return count;
    }
      
    // Driver Code
    public static void Main()
    {
        int []a = { 5, 4, 6, 1, 3, 1 };
        int n = a.Length;
      
        Console.WriteLine(getPositionCount(a, n));
    }
}
  
// This code is contributed by AnkitRai01


输出:
4

时间复杂度: O(N)