📌  相关文章
📜  计算 Array 中存在严格更小和严格更大的元素的元素

📅  最后修改于: 2022-05-13 01:56:10.608000             🧑  作者: Mango

计算 Array 中存在严格更小和严格更大的元素的元素

给定一个数组arr[] ,任务是找到给定数组中元素的计数,使得存在一个严格小于它的元素和一个严格大于它的元素。

例子:

朴素方法:给定问题可以通过遍历数组arr[]的每个元素并检查是否存在严格大于和严格小于它的元素来解决。

时间复杂度: O(N 2 )
辅助空间: O(1)

高效方法:上述方法可以通过查找给定数组的最小和最大元素、遍历给定数组arr[]并检查arr[i]是否严格大于最小值并严格小于最大值来进行优化。将此类索引的计数保留在所需答案的变量中。

下面是上述方法的实现:

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& 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 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


输出
2

时间复杂度: O(N)
辅助空间: O(1)