📌  相关文章
📜  满足给定条件的数组中的索引计数

📅  最后修改于: 2021-05-06 17:28:38             🧑  作者: Mango

给定一个由N个正整数组成的数组arr [] ,任务是找到索引i的数量,以使从arr [0]arr [i – 1]的所有元素都小于arr [i]

例子:

方法:想法是从左到右遍历数组,并跟踪当前最大值,只要此最大值发生变化,则当前索引为有效索引,因此增加结果计数器。

下面是上述方法的实现:

C++
// C++ implementation of the approach
#include 
using namespace std;
  
// Function to return the count
// of indices that satisfy
// the given condition
int countIndices(int arr[], int n)
{
  
    // To store the result
    int cnt = 0;
  
    // To store the current maximum
    // Initialized to 0 since there are only
    // positive elements in the array
    int max = 0;
    for (int i = 0; i < n; i++) {
  
        // i is a valid index
        if (max < arr[i]) {
  
            // Update the maximum so far
            max = arr[i];
  
            // Increment the counter
            cnt++;
        }
    }
  
    return cnt;
}
  
// Driver code
int main()
{
    int arr[] = { 1, 2, 3, 4 };
    int n = sizeof(arr) / sizeof(int);
  
    cout << countIndices(arr, n);
  
    return 0;
}


Java
// Java implementation of the approach
class GFG 
{
  
// Function to return the count
// of indices that satisfy
// the given condition
static int countIndices(int arr[], int n)
{
  
    // To store the result
    int cnt = 0;
  
    // To store the current maximum
    // Initialized to 0 since there are only
    // positive elements in the array
    int max = 0;
    for (int i = 0; i < n; i++) 
    {
  
        // i is a valid index
        if (max < arr[i]) 
        {
  
            // Update the maximum so far
            max = arr[i];
  
            // Increment the counter
            cnt++;
        }
    }
    return cnt;
}
  
// Driver code
public static void main(String[] args) 
{
    int arr[] = { 1, 2, 3, 4 };
    int n = arr.length;
  
    System.out.println(countIndices(arr, n));
}
}
  
// This code is contributed by Rajput-Ji


Python3
# Python implementation of the approach
  
# Function to return the count
# of indices that satisfy
# the given condition
def countIndices(arr, n):
  
    # To store the result
    cnt = 0;
  
    # To store the current maximum
    # Initialized to 0 since there are only
    # positive elements in the array
    max = 0;
    for i in range(n):
        # i is a valid index
        if (max < arr[i]): 
  
            # Update the maximum so far
            max = arr[i];
  
            # Increment the counter
            cnt += 1;
  
    return cnt;
  
# Driver code
if __name__ == '__main__':
    arr = [ 1, 2, 3, 4 ];
    n = len(arr);
  
    print(countIndices(arr, n));
  
# This code is contributed by 29AjayKumar


C#
// C# implementation of the approach
using System;
      
class GFG 
{
  
// Function to return the count
// of indices that satisfy
// the given condition
static int countIndices(int []arr, int n)
{
  
    // To store the result
    int cnt = 0;
  
    // To store the current maximum
    // Initialized to 0 since there are only
    // positive elements in the array
    int max = 0;
    for (int i = 0; i < n; i++) 
    {
  
        // i is a valid index
        if (max < arr[i]) 
        {
  
            // Update the maximum so far
            max = arr[i];
  
            // Increment the counter
            cnt++;
        }
    }
    return cnt;
}
  
// Driver code
public static void Main(String[] args) 
{
    int []arr = { 1, 2, 3, 4 };
    int n = arr.Length;
  
    Console.WriteLine(countIndices(arr, n));
}
}
  
// This code is contributed by PrinciRaj1992


输出:
4