📌  相关文章
📜  计算超出所有先前元素和下一数组元素的数组元素

📅  最后修改于: 2021-05-18 00:03:31             🧑  作者: Mango

给定一个数组arr [] ,任务是查找满足以下条件的数组元素的数量:

  • 数组元素应严格大于所有先前出现的数组元素。
  • 它是最后一个数组元素,或者整数应严格大于下一个数组元素。

注意:也可以考虑数组的第一个整数。

例子:

方法:想法是线性遍历数组并检查每个数组元素是否满足给定条件。请按照以下步骤解决此问题:

  • 遍历数组。
  • 从数组的第一个元素开始,跟踪到目前为止遇到的最大数组元素。
  • 如果最大数组元素大于先前遇到的最大数组元素,则更新它。
  • 更新当前最大值后,检查下一个数组元素是否大于当前数组元素。如果发现为真,则增加计数。
  • 重复此过程,直到遍历最后一个数组元素。
  • 最后,打印获得的计数。

下面是上述方法的实现:

C++
// C++ program for the above approach
 
#include 
using namespace std;
 
// Function to count array elements
// satisfying the given condition
int numberOfIntegers(int arr[], int N)
{
    int cur_max = 0, count = 0;
 
    // If there is only one
    // array element
    if (N == 1) {
        count = 1;
    }
    else {
 
        // Traverse the array
        for (int i = 0; i < N - 1; i++) {
 
            // Update the maximum element
            // encountered so far
            if (arr[i] > cur_max) {
                cur_max = arr[i];
 
                // Count the number of array elements
                // strictly greater than all previous
                // and immediately next elements
                if (arr[i] > arr[i + 1]) {
 
                    count++;
                }
            }
        }
        if (arr[N - 1] > cur_max)
            count++;
    }
 
    // Print the count
    cout << count;
}
 
// Driver Code
int main()
{
    // Given array
    int arr[] = { 1, 2, 0, 7, 2, 0, 2, 0 };
 
    // Size of the array
    int N = sizeof(arr) / sizeof(arr[0]);
 
    numberOfIntegers(arr, N);
 
    return 0;
}


Java
// Java program for the above approach
import java.io.*;
class GFG {
 
    // Function to count array elements
    // satisfying the given condition
    static void numberOfIntegers(int[] arr, int N)
    {
        int cur_max = 0, count = 0;
 
        // If there is only one
        // array element
        if (N == 1) {
            count = 1;
        }
        else
        {
 
            // Traverse the array
            for (int i = 0; i < N - 1; i++)
            {
 
                // Update the maximum element
                // encountered so far
                if (arr[i] > cur_max)
                {
                    cur_max = arr[i];
 
                    // Count the number of array elements
                    // strictly greater than all previous
                    // and immediately next elements
                    if (arr[i] > arr[i + 1])
                    {
                        count++;
                    }
                }
            }
            if (arr[N - 1] > cur_max)
                count++;
        }
 
        // Print the count
        System.out.println(count);
    }
 
    // Driver Code
    public static void main(String[] args)
    {
 
        // Given array
        int[] arr = new int[] { 1, 2, 0, 7, 2, 0, 2, 0 };
 
        // Size of the array
        int N = arr.length;
        numberOfIntegers(arr, N);
    }
}
 
// This code is contributed by dharanendralv23


Python3
# Python program for the above approach
 
# Function to count array elements
# satisfying the given condition
def numberOfIntegers(arr, N) :  
    cur_max = 0
    count = 0
 
    # If there is only one
    # array element
    if (N == 1) :
        count = 1   
    else :
 
        # Traverse the array
        for i in range(N - 1):
 
            # Update the maximum element
            # encountered so far
            if (arr[i] > cur_max) :
                cur_max = arr[i]
 
                # Count the number of array elements
                # strictly greater than all previous
                # and immediately next elements
                if (arr[i] > arr[i + 1]) :
                    count += 1                  
        if (arr[N - 1] > cur_max) :
            count += 1
     
    # Prthe count
    print(count)
 
# Driver Code
 
# Given array
arr = [ 1, 2, 0, 7, 2, 0, 2, 0 ]
 
# Size of the array
N = len(arr)
numberOfIntegers(arr, N)
 
# This code is contributed by sanjoy_62.


C#
// C# program for the above approach
using System;
class GFG
{
 
    // Function to count array elements
    // satisfying the given condition
    static void numberOfIntegers(int[] arr, int N)
    {
        int cur_max = 0, count = 0;
 
        // If there is only one
        // array element
        if (N == 1) {
            count = 1;
        }
        else {
 
            // Traverse the array
            for (int i = 0; i < N - 1; i++) {
 
                // Update the maximum element
                // encountered so far
                if (arr[i] > cur_max) {
                    cur_max = arr[i];
 
                    // Count the number of array elements
                    // strictly greater than all previous
                    // and immediately next elements
                    if (arr[i] > arr[i + 1]) {
 
                        count++;
                    }
                }
            }
            if (arr[N - 1] > cur_max)
                count++;
        }
 
        // Print the count
        Console.WriteLine(count);
    }
 
    // Driver Code
    static public void Main()
    {
 
        // Given array
        int[] arr = new int[] { 1, 2, 0, 7, 2, 0, 2, 0 };
 
        // Size of the array
        int N = arr.Length;
 
        numberOfIntegers(arr, N);
    }
}
 
// This code is contributed by dharavendralv23


Javascript


输出:
2

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