给定一个数组arr[] ,任务是找到满足以下条件的数组元素的数量:
- 数组元素应严格大于所有先前出现的数组元素。
- 它要么是最后一个数组元素,要么整数应该严格大于下一个数组元素。
注意:也可以考虑数组的第一个整数。
例子:
Input: arr[] = {1, 2, 0, 7, 2, 0, 2, 0}
Output: 2
Explanation: arr[1] (= 2) and arr[3] ( = 7) are the array elements satisfying the given condition.
Input: arr[] = {4, 8, 15, 16, 23, 42}
Output: 1
方法:思想是线性遍历数组并检查每个数组元素是否满足给定条件。请按照以下步骤解决此问题:
- 遍历数组。
- 从数组的第一个元素开始,跟踪迄今为止遇到的最大数组元素。
- 如果最大数组元素大于之前遇到的最大数组元素,则更新最大数组元素。
- 更新当前最大值后,检查下一个数组元素是否大于当前数组元素。如果发现为真,则增加计数。
- 重复这个过程,直到遍历到最后一个数组元素。
- 最后,打印获得的计数。
以下是上述方法的实现:
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
# Print the 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)
如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live