Open In App

Count of elements which are second smallest among three consecutive elements

Last Updated : 07 Mar, 2022
Comments
Improve
Suggest changes
Like Article
Like
Report

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 <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;
}
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
<script>

// JavaScript 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]
    function countElements(p , n)
    {
        // To store the required answer
        var ans = 0;

        // Traverse from the second element
        // to the second last element
        for (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
    
        var p = [ 2, 5, 1, 3, 4 ];
        var n = p.length;

        document.write(countElements(p, n));

// This code contributed by Rajput-Ji 

</script>

Output: 
1

 

Time Complexity: O(n)

Auxiliary Space: O(1)


Next Article
Practice Tags :

Similar Reads