📜  在三个连续元素中倒数第二个的元素计数

📅  最后修改于: 2021-04-24 18:42:18             🧑  作者: Mango

给定前N个自然数的排列P。任务是找到元素P i的数量,以使P iP i – 1 ,P i和P i +1中仅次于第二。
例子:

方法:遍历从1N – 2的排列(从零开始的索引),并检查以下两个条件。如果这些条件中的任何一个满足,则增加所需的答案。

  • 如果P [i – 1]

  • 如果P [i – 1]> P [i]> P [i + 1]

下面是上述方法的实现:

C++
// C++ implementation of the approach
#include 
using namespace std;
 
// Function to return the count of elements
// P[i] such that P[i] is the second smallest
// among P[i – 1], P[i] and P[i + 1]
int countElements(int p[], int n)
{
    // To store the required answer
    int ans = 0;
 
    // Traverse from the second element
    // to the second last element
    for (int i = 1; i < n - 1; i++) {
        if (p[i - 1] > p[i] and p[i] > p[i + 1])
            ans++;
        else if (p[i - 1] < p[i] and p[i] < p[i + 1])
            ans++;
    }
 
    // Return the required answer
    return ans;
}
 
// Driver code
int main()
{
    int p[] = { 2, 5, 1, 3, 4 };
    int n = sizeof(p) / sizeof(p[0]);
 
    cout << countElements(p, n);
 
    return 0;
}


Java
// Java implementation of the approach
class GFG
{
 
// Function to return the count of elements
// P[i] such that P[i] is the second smallest
// among P[i-1], P[i] and P[i + 1]
static int countElements(int p[], int n)
{
    // To store the required answer
    int ans = 0;
 
    // Traverse from the second element
    // to the second last element
    for (int i = 1; i < n - 1; i++)
    {
        if (p[i - 1] > p[i] && p[i] > p[i + 1])
            ans++;
        else if (p[i - 1] < p[i] && p[i] < p[i + 1])
            ans++;
    }
 
    // Return the required answer
    return ans;
}
 
// Driver code
public static void main(String []args)
{
    int p[] = { 2, 5, 1, 3, 4 };
    int n = p.length;
 
    System.out.println(countElements(p, n));
}
}
 
// This code is contributed by PrinciRaj1992


Python3
# Python3 implementation of the approach
 
# Function to return the count of elements
# P[i] such that P[i] is the second smallest
# among P[i – 1], P[i] and P[i + 1]
def countElements(p, n) :
 
    # To store the required answer
    ans = 0;
 
    # Traverse from the second element
    # to the second last element
    for i in range(1, n - 1) :
         
        if (p[i - 1] > p[i] and p[i] > p[i + 1]) :
            ans += 1;
        elif (p[i - 1] < p[i] and p[i] < p[i + 1]) :
            ans += 1;
     
    # Return the required answer
    return ans;
 
# Driver code
if __name__ == "__main__" :
 
    p = [ 2, 5, 1, 3, 4 ];
    n = len(p);
 
    print(countElements(p, n));
 
# This code is contributed by AnkitRai01


C#
// C# implementation of the approach
using System;
 
class GFG
{
 
// Function to return the count of elements
// P[i] such that P[i] is the second smallest
// among P[i-1], P[i] and P[i + 1]
static int countElements(int []p, int n)
{
    // To store the required answer
    int ans = 0;
 
    // Traverse from the second element
    // to the second last element
    for (int i = 1; i < n - 1; i++)
    {
        if (p[i - 1] > p[i] && p[i] > p[i + 1])
            ans++;
        else if (p[i - 1] < p[i] && p[i] < p[i + 1])
            ans++;
    }
 
    // Return the required answer
    return ans;
}
 
// Driver code
public static void Main(String []args)
{
    int []p = { 2, 5, 1, 3, 4 };
    int n = p.Length;
 
    Console.WriteLine(countElements(p, n));
}
}
 
// This code is contributed by Rajput-Ji


Javascript


输出:
1