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

• 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 = 7, arr is strictly greater than it and arr is strictly smaller than it. Similarly for arr, arr is strictly greater than it and arr 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 ``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;` `    ``// Stores the minimum``    ``int` `b = arr;``    ``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

 ``

Output

`2`

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

My Personal Notes arrow_drop_up