Given a permutation **P** of first **N** natural numbers. The task is to find the number of elements **P _{i}** such that

**P**is second smallest among

_{i}**P**.

_{i – 1}, P_{i}and P_{i + 1}**Examples:**

Input:P[] = {2, 5, 1, 3, 4}

Output:1

3 is the only such element.

Input:P[] = {1, 2, 3, 4}

Output:2

**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 <bits/stdc++.h> ` `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; ` `} ` |

*chevron_right*

*filter_none*

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

*chevron_right*

*filter_none*

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

*chevron_right*

*filter_none*

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

*chevron_right*

*filter_none*

**Output:**

1

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Program to find the smallest element among three elements
- Print numbers such that no two consecutive numbers are co-prime and every three consecutive numbers are co-prime
- Find the lexicographically smallest sequence which can be formed by re-arranging elements of second array
- Smallest multiple of 3 which consists of three given non-zero digits
- Probability of getting two consecutive heads after choosing a random coin among two different types of coins
- Number of quadruples where the first three terms are in AP and last three terms are in GP
- Count number of rotated strings which have more number of vowels in the first half than second half
- Length of second longest sequence of consecutive 1s in a binary array
- Count possible binary strings of length N without P consecutive 0s and Q consecutive 1s
- Check if a number can be written as sum of three consecutive integers
- Prove that atleast one of three consecutive even numbers is divisible by 6
- Frequency of smallest character in first sentence less than that of second sentence
- Number of hours after which the second person moves ahead of the first person if they travel at a given speed
- Find numbers which are multiples of first array and factors of second array
- Smallest n digit number divisible by given three numbers
- Count ways to distribute m items among n people
- Count of groups among N people having only one leader in each group
- Count of ways to distribute N items among 3 people with one person receiving maximum
- Smallest subsequence with sum of absolute difference of consecutive elements maximized
- Generate a Binary String without any consecutive 0's and at most K consecutive 1's

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.