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

A number is strictly a smaller element means the number should be less than by a minimum difference is 1 and similarly strictly greater element means the number should be greater than by a minimum of difference 1. Here we have given an array of integers of size n and we have to return the count of elements in the array having strictly smaller and strictly greater elements present.

Let's see examples with explanations below to understand the problem in a better way.

## Sample Examples

Input

N = 5
Array: [ 3, 2, 1, 4, 5 ]


Output

3

Explanation: In the above array:

Array[0] has both elements array[3] as strictly greater and array[1] as strictly smaller.

Similarly, Array[1] has both elements array[0] as strictly greater and array[2] as strictly smaller.

Similarly, Array[3] has both elements array[2] as strictly smaller and array[4] as strictly greater.

Input

N = 3
Array: [ 2, 2, 6 ]


Output

0

Explanation: In the above array there is no index have both elements strictly greater and strictly smaller.

## Naive Approach

In this approach, we have a Traverse array using a nested for loop and check each element for having strictly smaller and strictly greater elements present or not. And store the count of elements according to the condition and in the end return it.

Let's see the code below for a better understanding of the above approach.

### Example

C++ code for the Count elements in Array having strictly smaller and strictly greater elements present

#include <bits/stdc++.h>
using namespace std;
//Create a function to count elements in the array
int elementsCount(int N, int array[]) {
int resCount = 0;   //Store the final ans
//Create a bool element to check strictly smaller and greater elements
bool strictlySmalleElement;
bool strictlyGreaterElement;
//Iterate the array using for loop
for (int i = 0; i < N; i++) {
strictlySmalleElement = false;
strictlyGreaterElement = false;
for (int j = 0; j < N; j++) {
if (i != j) {
// check for the smaller element
if (array[j] < array[i])
strictlySmalleElement = true;
// check for the greater element
else if (array[j] > array[i])
strictlyGreaterElement = true;
}
}
//count the element which has both strictly smaller and greater elements
if (strictlySmalleElement && strictlyGreaterElement)
resCount++; //Increase the count
}
return resCount;
}
int main() {
int array[] = { 3, 2, 1, 4, 5 }; //Given array
int N = sizeof(array) / sizeof(array[0]); //Getting the size of the array
cout << "Count of Elements having strictly greater and smaller elements: ";
cout<< elementsCount(N, array);
return 0;
}


### Output

Count of Elements having strictly greater and smaller elements: 3


### Time and Space Complexity

The time complexity of the above code is O(N^2), as we use the nested for loop. Where N is the size of the string.

The space complexity of the above code is O(1), as we are not using any extra space.

## Optimize Approach

In this approach first, we find the maximum and minimum element of the given array using for loop and then again traverse the given array and check for each element is smaller than the maximum element and larger than the minimum element then increases the count and return it.

Let's see the code below for a better understanding of the above approach.

### Example

C++ code for the Count elements in Array having strictly smaller and strictly greater elements present.

#include <bits/stdc++.h>
using namespace std;
// Create a function to count elements in the array
int elementsCount(int N, int array[]){
// Create maxElement to store the maximum number and initialized it with INT_MIN
int maxElement = INT_MIN;
//Create minElement to store minimum number and initialized it with INT_MAX
int minElement = INT_MAX;
for( int i=0; i<N; i++ ){
maxElement = max(maxElement, array[i]); // to get the maximum element
minElement = min(minElement, array[i]); // to get the minimum element
}
int resCount = 0; // Store the final ans
// Traverse the for loop to update resCount
for (int i=0; i<N; i++) {
// Check if current element is less than maximum element and greater than the minimum element
if (array[i] < maxElement && array[i] > minElement){
resCount++; // Increase the count
}
}
return resCount;
}
int main(){
int array[] = { 3, 2, 1, 4, 5 }; //Given array
int N = sizeof(array) / sizeof(array[0]); //Getting the size of the array
cout << "Count of Elements having strictly greater and smaller elements: ";
cout<< elementsCount(N, array);
return 0;
}


### Output

Count of Elements having strictly greater and smaller elements: 3


## Time and Space Complexity

The time complexity of the above code is O(N), as we only traverse the given array. Where N is the size of the string.

The space complexity of the above code is O(1), as we are not using any extra space.

## Conclusion

In this tutorial, we have implemented a C++ program to find the Count elements in Array having strictly smaller and strictly greater elements present. We have implemented two approaches naive approach and optimize approach. The time complexity is O(N^2) and O(N) respectively. Where N is the size of the array. And space complexity of both approaches is O(1).

Updated on: 31-Aug-2023

33 Views