# Count of elements which are second smallest among three consecutive elements

• Last Updated : 07 Mar, 2022

Given a permutation P of first N natural numbers. The task is to find the number of elements Pi such that Pi is second smallest among Pi – 1, Pi and Pi + 1.
Examples:

Input: P[] = {2, 5, 1, 3, 4}
Output:
3 is the only such element.
Input: P[] = {1, 2, 3, 4}
Output:

Approach: Traverse the permutation from 1 to N – 2 ( zero-based indexing) and check the below two conditions. If anyone of these conditions satisfy then increment the required answer.

• If P[i – 1] < P[i] < P[i + 1].
• If P[i – 1] > P[i] > P[i + 1].

Below is the implementation of the above approach:

## 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);` `    ``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

 ``

Output:

`1`

Time Complexity: O(n)

Auxiliary Space: O(1)

My Personal Notes arrow_drop_up