📌  相关文章
📜  Array 中所有前缀元素少于后缀中的所有元素的索引计数

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

Array 中所有前缀元素少于后缀中的所有元素的索引计数

给定一个数组arr[] ,任务是计算索引的总数,其中左侧的所有元素都小于数组右侧的所有元素。

例子

方法:该方法基于以下思想:

请按照以下步骤解决给定的问题:

  • 初始化Max = INT_MIN、 Min = INT_MAX 和Count = 0
  • 现在,创建大小为N左右两个数组。
  • 从头到尾运行一个循环。
    • 在每次迭代中将 Max 更新为Max = max(Max, arr[i]) 并分配 left[i] = Max
  • 从头到尾运行另一个循环。
    • 在每次迭代中将 Min 更新为Min = min(Min, arr[i]) 并分配 right[i] = Min
  • 从头到尾遍历数组。
  • 如果left[i] <= right[i+1] ,则达到排序点,
    • 计数增加 1

下面是上述方法的实现:

C++
// C++ program for the above approach
 
#include 
using namespace std;
 
// Function to return total count
// of sorted points in the array
int countSortedPoints(int* arr, int N)
{
 
    int left[N];
    int right[N];
 
    // Initialize the variables
    int Min = INT_MAX;
    int Max = INT_MIN;
    int Count = 0;
 
    // Make Maximum array
    for (int i = 0; i < N; i++) {
 
        Max = max(arr[i], Max);
        left[i] = Max;
    }
 
    // Make Minimum array
    for (int i = N - 1; i >= 0; i--) {
 
        Min = min(arr[i], Min);
        right[i] = Min;
    }
 
    // Count of sorted points
    for (int i = 0; i < N - 1; i++) {
        if (left[i] <= right[i + 1])
            Count++;
    }
 
    // Return count of sorted points
    return Count;
}
 
// Driver Code
int main()
{
    int arr[] = { 1, 5, 4, 2, 3, 8, 7, 9 };
    int N = sizeof(arr) / sizeof(arr[0]);
 
    // Function call
    cout << countSortedPoints(arr, N);
    return 0;
}


Java
// Java program for the above approach
import java.io.*;
class GFG {
 
  // Function to return total count
  // of sorted points in the array
  static int countSortedPoints(int []arr, int N)
  {
 
    int []left = new int[N];
    int []right = new int[N];
 
    // Initialize the variables
    int Min = Integer.MAX_VALUE;
    int Max = Integer.MIN_VALUE;
    int Count = 0;
 
    // Make Maximum array
    for (int i = 0; i < N; i++) {
 
      Max = Math.max(arr[i], Max);
      left[i] = Max;
    }
 
    // Make Minimum array
    for (int i = N - 1; i >= 0; i--) {
 
      Min = Math.min(arr[i], Min);
      right[i] = Min;
    }
 
    // Count of sorted points
    for (int i = 0; i < N - 1; i++) {
      if (left[i] <= right[i + 1])
        Count++;
    }
 
    // Return count of sorted points
    return Count;
  }
 
  // Driver Code
  public static void main (String[] args) {
    int arr[] = { 1, 5, 4, 2, 3, 8, 7, 9 };
    int N = arr.length;
 
    // Function call
    System.out.print(countSortedPoints(arr, N));
  }
}
 
// This code is contributed by hrithikgarg03188.


Python3
# Python3 implementation of the approach
INT_MIN = -2147483648
INT_MAX = 2147483647
 
# Function to return total count
# of sorted points in the array
def countSortedPoints(arr, N):
 
    left = [0 for i in range(N)]
    right = [0 for i in range(N)]
 
    # Initialize the variables
    Min = INT_MAX
    Max = INT_MIN
    Count = 0
 
    # Make Maximum array
    for i in range(N):
 
        Max = max(arr[i], Max)
        left[i] = Max
 
            # Make Minimum array
    for i in range(N - 1, -1, -1):
 
        Min = min(arr[i], Min)
        right[i] = Min
 
    # Count of sorted points
    for i in range(0, N - 1):
        if (left[i] <= right[i + 1]):
            Count += 1
 
            # Return count of sorted points
    return Count
 
# Driver Code
arr = [1, 5, 4, 2, 3, 8, 7, 9]
N = len(arr)
 
# Function call
print(countSortedPoints(arr, N))
 
# This code is contributed by shinjanpatra


C#
// C# program for the above approach
using System;
class GFG
{
 
// Function to return total count
// of sorted points in the array
static int countSortedPoints(int []arr, int N)
{
 
    int []left = new int[N];
    int []right = new int[N];
 
    // Initialize the variables
    int Min = Int32.MaxValue;
    int Max = Int32.MinValue;
    int Count = 0;
 
    // Make Maximum array
    for (int i = 0; i < N; i++) {
 
        Max = Math.Max(arr[i], Max);
        left[i] = Max;
    }
 
    // Make Minimum array
    for (int i = N - 1; i >= 0; i--) {
 
        Min = Math.Min(arr[i], Min);
        right[i] = Min;
    }
 
    // Count of sorted points
    for (int i = 0; i < N - 1; i++) {
        if (left[i] <= right[i + 1])
            Count++;
    }
 
    // Return count of sorted points
    return Count;
}
 
// Driver Code
public static void Main()
{
    int []arr = { 1, 5, 4, 2, 3, 8, 7, 9 };
    int N = arr.Length;
 
    // Function call
    Console.Write(countSortedPoints(arr, N));
}
}
 
// This code is contributed by Samim Hossain Mondal.


Javascript



输出
3

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