📌  相关文章
📜  子数组的计数,这些子数组根据给定的Array元素形成排列

📅  最后修改于: 2021-05-07 06:40:45             🧑  作者: Mango

给定的阵列A []由整数的[1,N],任务是要计算所有可能的长度×(1≤X≤N)的子阵列的总数,其由整数[1,x]中从一个排列的给定的数组。
例子:

天真的方法:
请按照以下步骤解决问题:

  • 解决问题的最简单方法是生成所有可能的子数组。
  • 对于每个子数组,检查它是否是[1,subarray length]范围内的元素排列。
  • 对于找到的每个这样的子数组,增加count 。最后,打印计数

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

高效方法:
要优化上述方法,请按照以下步骤操作:

  • 对于i = [1,N]中的每个元素,检查最大最小索引,其中存在置换[1,i]的元素。
  • 如果最大索引最小索引之间的差等于i ,则意味着存在针对i的有效连续排列。
  • 对于每个这样的排列,增加count 。最后,打印计数

下面是上述方法的实现:

C++
// C++ Program to implement
// the above approach
#include 
using namespace std;
  
// Function returns the required count
int PermuteTheArray(int A[], int n)
{
  
    int arr[n];
  
    // Store the indices of the
    // elements present in A[].
    for (int i = 0; i < n; i++) {
        arr[A[i] - 1] = i;
    }
  
    // Store the maximum and
    // minimum index of the
    // elements from 1 to i.
    int mini = n, maxi = 0;
    int count = 0;
  
    for (int i = 0; i < n; i++) {
  
        // Update maxi and mini, to
        // store minimum and maximum
        // index for permutation
        // of elements from 1 to i+1
        mini = min(mini, arr[i]);
        maxi = max(maxi, arr[i]);
  
        // If difference between maxi
        // and mini is equal to i
        if (maxi - mini == i)
  
            // Increase count
            count++;
    }
  
    // Return final count
    return count;
}
  
// Driver Code
int main()
{
  
    int A[] = { 4, 5, 1, 3, 2, 6 };
    cout << PermuteTheArray(A, 6);
  
    return 0;
}


Java
// Java program to implement
// the above approach
class GFG{
  
// Function returns the required count
static int PermuteTheArray(int A[], int n)
{
    int []arr = new int[n];
  
    // Store the indices of the
    // elements present in A[].
    for(int i = 0; i < n; i++) 
    {
        arr[A[i] - 1] = i;
    }
  
    // Store the maximum and
    // minimum index of the
    // elements from 1 to i.
    int mini = n, maxi = 0;
    int count = 0;
  
    for(int i = 0; i < n; i++)
    {
  
        // Update maxi and mini, to
        // store minimum and maximum
        // index for permutation
        // of elements from 1 to i+1
        mini = Math.min(mini, arr[i]);
        maxi = Math.max(maxi, arr[i]);
  
        // If difference between maxi
        // and mini is equal to i
        if (maxi - mini == i)
  
            // Increase count
            count++;
    }
  
    // Return final count
    return count;
}
  
// Driver Code
public static void main(String[] args)
{
    int A[] = { 4, 5, 1, 3, 2, 6 };
      
    System.out.print(PermuteTheArray(A, 6));
}
}
  
// This code is contributed by gauravrajput1


Python3
# Python3 program to implement
# the above approach
  
# Function returns the required count
def PermuteTheArray(A, n):
  
    arr = [0] * n
  
    # Store the indices of the
    # elements present in A[].
    for i in range(n):
        arr[A[i] - 1] = i
  
    # Store the maximum and
    # minimum index of the
    # elements from 1 to i.
    mini = n
    maxi = 0
    count = 0
  
    for i in range(n):
  
        # Update maxi and mini, to
        # store minimum and maximum
        # index for permutation
        # of elements from 1 to i+1
        mini = min(mini, arr[i])
        maxi = max(maxi, arr[i])
  
        # If difference between maxi
        # and mini is equal to i
        if (maxi - mini == i):
  
            # Increase count
            count += 1
  
    # Return final count
    return count
  
# Driver Code
if __name__ == "__main__":
  
    A = [ 4, 5, 1, 3, 2, 6 ]
      
    print(PermuteTheArray(A, 6))
  
# This code is contributed by chitranayal


C#
// C# program to implement
// the above approach
using System;
using System.Collections.Generic;
  
class GFG{
  
// Function returns the required count
static int PermuteTheArray(int []A, int n)
{
    int []arr = new int[n];
  
    // Store the indices of the
    // elements present in []A.
    for(int i = 0; i < n; i++) 
    {
        arr[A[i] - 1] = i;
    }
  
    // Store the maximum and
    // minimum index of the
    // elements from 1 to i.
    int mini = n, maxi = 0;
    int count = 0;
  
    for(int i = 0; i < n; i++)
    {
  
        // Update maxi and mini, to
        // store minimum and maximum
        // index for permutation
        // of elements from 1 to i+1
        mini = Math.Min(mini, arr[i]);
        maxi = Math.Max(maxi, arr[i]);
  
        // If difference between maxi
        // and mini is equal to i
        if (maxi - mini == i)
  
            // Increase count
            count++;
    }
  
    // Return final count
    return count;
}
  
// Driver Code
public static void Main(String[] args)
{
    int []A = { 4, 5, 1, 3, 2, 6 };
      
    Console.Write(PermuteTheArray(A, 6));
}
}
  
// This code is contributed by gauravrajput1


输出:
4

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