Count elements in Array having strictly smaller and strictly greater element present
Last Updated :
11 Oct, 2023
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.
Algorithm:
- Initialize a variable ‘count‘ to 0 to keep track of the number of elements that satisfy the condition.
- Traverse each element of the array using a loop with index ‘i’.
- For each element, traverse the array again using a nested loop with index ‘j’ and check if there exists an element strictly smaller and an element strictly greater than the current element arr[i].
- If such elements exist, increase the ‘count‘ variable.
- Return the final value of ‘count‘.
Below is the implementation of the approach:
C++
#include <bits/stdc++.h>
using namespace std;
int countElements( int arr[], int n) {
int count = 0;
for ( int i = 0; i < n; i++) {
bool smaller = false , greater = false ;
for ( int j = 0; j < n; j++) {
if (i != j) {
if (arr[j] < arr[i])
smaller = true ;
else if (arr[j] > arr[i])
greater = true ;
}
}
if (smaller && greater)
count++;
}
return count;
}
int main() {
int arr[] = { 11, 7, 2, 15 };
int n = sizeof (arr) / sizeof (arr[0]);
int count = countElements(arr, n);
cout << count;
return 0;
}
|
Java
public class Main {
public static int countElements( int [] arr) {
int n = arr.length;
int count = 0 ;
for ( int i = 0 ; i < n; i++) {
boolean smaller = false , greater = false ;
for ( int j = 0 ; j < n; j++) {
if (i != j) {
if (arr[j] < arr[i])
smaller = true ;
else if (arr[j] > arr[i])
greater = true ;
}
}
if (smaller && greater)
count++;
}
return count;
}
public static void main(String[] args) {
int [] arr = { 11 , 7 , 2 , 15 };
int count = countElements(arr);
System.out.println(count);
}
}
|
Python3
def count_elements(arr):
count = 0
for i in range ( len (arr)):
smaller, greater = False , False
for j in range ( len (arr)):
if i ! = j:
if arr[j] < arr[i]:
smaller = True
elif arr[j] > arr[i]:
greater = True
if smaller and greater:
count + = 1
return count
arr = [ 11 , 7 , 2 , 15 ]
count = count_elements(arr)
print (count)
|
C#
using System;
public class GFG {
public static int CountElements( int [] arr, int n)
{
int count = 0;
for ( int i = 0; i < n; i++) {
bool smaller = false , greater = false ;
for ( int j = 0; j < n; j++) {
if (i != j) {
if (arr[j] < arr[i])
smaller = true ;
else if (arr[j] > arr[i])
greater = true ;
}
}
if (smaller && greater)
count++;
}
return count;
}
public static void Main()
{
int [] arr = { 11, 7, 2, 15 };
int n = arr.Length;
int count = CountElements(arr, n);
Console.WriteLine(count);
}
}
|
Javascript
function countElements(arr, n) {
let count = 0;
for (let i = 0; i < n; i++) {
let smaller = false , greater = false ;
for (let j = 0; j < n; j++) {
if (i != j) {
if (arr[j] < arr[i])
smaller = true ;
else if (arr[j] > arr[i])
greater = true ;
}
}
if (smaller && greater)
count++;
}
return count;
}
let arr = [ 11, 7, 2, 15 ];
let n = arr.length;
let count = countElements(arr, n);
console.log(count);
|
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++
#include <bits/stdc++.h>
using namespace std;
int cntElements(vector< int >& arr)
{
int a = *max_element(
arr.begin(), arr.end());
int b = *min_element(
arr.begin(), arr.end());
int cnt = 0;
for ( auto x : arr) {
if (x < a && x > b)
cnt++;
}
return cnt;
}
int main()
{
vector< int > arr = { 11, 7, 2, 15 };
cout << cntElements(arr);
return 0;
}
|
Java
import java.util.*;
public class GFG
{
static int cntElements( int [] arr)
{
int cnt = 0 ;
int a = arr[ 0 ];
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];
}
}
for ( int i = 0 ; i < arr.length; i++) {
if (arr[i] < a && arr[i] > b)
cnt++;
}
return cnt;
}
public static void main(String args[])
{
int [] arr = { 11 , 7 , 2 , 15 };
System.out.print(cntElements(arr));
}
}
|
Python3
def cntElements(arr):
a = max (arr)
b = min (arr)
cnt = 0
for x in range ( len (arr)):
if arr[x] < a and arr[x]> b:
cnt = cnt + 1
return cnt
arr = [ 11 , 7 , 2 , 15 ];
print (cntElements(arr));
|
C#
using System;
class GFG
{
static int cntElements( int [] arr)
{
int cnt = 0;
int a = arr[0];
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];
}
}
for ( int i = 0; i < arr.Length; i++) {
if (arr[i] < a && arr[i] > b)
cnt++;
}
return cnt;
}
public static int Main()
{
int [] arr = { 11, 7, 2, 15 };
Console.Write(cntElements(arr));
return 0;
}
}
|
Javascript
<script>
const cntElements = (arr) => {
let a = Math.max(...arr);
let b = Math.min(...arr);
let cnt = 0;
for (let x in arr) {
if (arr[x] < a && arr[x] > b)
cnt++;
}
return cnt;
}
let arr = [11, 7, 2, 15];
document.write(cntElements(arr));
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...