Count of elements which are second smallest among three consecutive elements

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: 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.

Below is the implementation of the above approach:

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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

edit
close

play_arrow

link
brightness_4
code

// 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

edit
close

play_arrow

link
brightness_4
code

# 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

edit
close

play_arrow

link
brightness_4
code

// 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

Output:
1



pawanasipugmailcom

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.





Article Tags :
Practice Tags :