Skip to content
Related Articles

Related Articles

Count elements in Array having strictly smaller and strictly greater element present

View Discussion
Improve Article
Save Article
Like Article
  • Last Updated : 13 Feb, 2022

Given an array arr[], the task is to find the count of elements in the given array such that there exists an element strictly smaller and an element strictly greater than it.

Examples:

Input: arr [] = {11, 7, 2, 15}
Output: 2
Explanation: For arr[1] = 7, arr[0] is strictly greater than it and arr[2] is strictly smaller than it. Similarly for arr[1], arr[3] is strictly greater than it and arr[2] is strictly smaller than it. Hence, the required count is 2.

Input: arr[] = {1, 1, 1, 3}
Output: 0

 

Naive Approach: The given problem can be solved by iterating over each element of the array arr[] and checking whether there exists a strictly greater and strictly smaller element than it.

Time Complexity: O(N2)
Auxiliary Space: O(1)

Efficient Approach: The above approach can be optimized by finding the minimum and maximum element of the given array, traversing the given array arr[], and checking if arr[i] is strictly greater than the minimum and strictly smaller than the maximum. Maintain the count of such indices in a variable which is the required answer.

Below is the implementation of the above approach:

C++




// C++ Program of the above approach
#include <bits/stdc++.h>
using namespace std;
 
// Function to find the count of elements
// in the given array such that there exists
// a strictly smaller and greater element
int cntElements(vector<int>& arr)
{
    // Stores the maximum
    int a = *max_element(
        arr.begin(), arr.end());
 
    // Stores the minimum
    int b = *min_element(
        arr.begin(), arr.end());
 
    // Stores the required count
    int cnt = 0;
 
    // Loop to iterate arr[]
    for (auto x : arr) {
        // If x is valid
        if (x < a && x > b)
            cnt++;
    }
 
    // Return Answer
    return cnt;
}
 
// Driver Code
int main()
{
    vector<int> arr = { 11, 7, 2, 15 };
    cout << cntElements(arr);
 
    return 0;
}

Java




// Java Program of the above approach
import java.util.*;
public class GFG
{
 
  // Function to find the count of elements
  // in the given array such that there exists
  // a strictly smaller and greater element
  static int cntElements(int[] arr)
  {
 
    // Stores the required count
    int cnt = 0;
 
    // Stores the maximum
    int a = arr[0];
 
    // Stores the minimum
    int b = arr[0];
    for (int i = 1; i < arr.length; i++) {
 
      if (arr[i] > a) {
        a = arr[i];
      }
 
      if (arr[i] < b) {
        b = arr[i];
      }
    }
    // Loop to iterate arr[]
    for (int i = 0; i < arr.length; i++) {
 
      // If x is valid
      if (arr[i] < a && arr[i] > b)
        cnt++;
    }
 
    // Return Answer
    return cnt;
  }
 
  // Driver Code
  public static void main(String args[])
  {
    int[] arr = { 11, 7, 2, 15 };
    System.out.print(cntElements(arr));
 
  }
}
 
// This code is contributed by Samim Hossain Mondal.

Python3




# Python code for the above approach
 
# Function to find the count of elements
# in the given array such that there exists
# a strictly smaller and greater element
def cntElements(arr):
 
    # Stores the maximum
    a = max(arr)
 
    # Stores the minimum
    b =min(arr)
 
    # Stores the required count
    cnt = 0
 
    # Loop to iterate arr[]
    for x in range(len(arr)):
        # If x is valid
        if arr[x] < a and arr[x]> b:
            cnt = cnt + 1
     
    # Return Answer
    return cnt
 
# Driver Code
arr = [11, 7, 2, 15];
print(cntElements(arr));
    
# This code is contributed by Potta Lokesh

C#




// C# Program of the above approach
using System;
class GFG
{
 
  // Function to find the count of elements
  // in the given array such that there exists
  // a strictly smaller and greater element
  static int cntElements(int[] arr)
  {
 
    // Stores the required count
    int cnt = 0;
 
    // Stores the maximum
    int a = arr[0];
 
    // Stores the minimum
    int b = arr[0];
    for (int i = 1; i < arr.Length; i++) {
 
      if (arr[i] > a) {
        a = arr[i];
      }
 
      if (arr[i] < b) {
        b = arr[i];
      }
    }
    // Loop to iterate arr[]
    for (int i = 0; i < arr.Length; i++) {
 
      // If x is valid
      if (arr[i] < a && arr[i] > b)
        cnt++;
    }
 
    // Return Answer
    return cnt;
  }
 
  // Driver Code
  public static int Main()
  {
    int[] arr = { 11, 7, 2, 15 };
    Console.Write(cntElements(arr));
 
    return 0;
  }
}
 
// This code is contributed by Taranpreet

Javascript




<script>
    // JavaScript Program of the above approach
 
    // Function to find the count of elements
    // in the given array such that there exists
    // a strictly smaller and greater element
    const cntElements = (arr) => {
        // Stores the maximum
        let a = Math.max(...arr);
 
        // Stores the minimum
        let b = Math.min(...arr);
 
        // Stores the required count
        let cnt = 0;
 
        // Loop to iterate arr[]
        for (let x in arr) {
            // If x is valid
            if (arr[x] < a && arr[x] > b)
                cnt++;
        }
 
        // Return Answer
        return cnt;
    }
 
    // Driver Code
 
    let arr = [11, 7, 2, 15];
    document.write(cntElements(arr));
 
// This code is contributed by rakeshsahni
 
</script>

Output

2

Time Complexity: O(N)
Auxiliary Space: O(1)


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!