📌  相关文章
📜  大小为K的子数组的计数,它是数字从1到K的排列

📅  最后修改于: 2021-05-17 22:11:47             🧑  作者: Mango

给定一个由不同整数组成的数组arr ,任务是查找大小为i的子数组的数量,该子数组具有从1到i的所有元素,换句话说,子数组是元素从1到i的任何排列,其中1 <= i <= N。
例子:

幼稚的方法是从每个索引开始,尝试找到每个大小(i)的子数组,并检查是否存在从1到i的所有元素。

时间复杂度:O(N 2 )

通过检查是否有可能为i的每个值(从1到N)创建大小为i的子数组,从而提供一种有效的方法
我们知道大小为K的每个子数组必须是从1到K的所有元素的排列,知道我们可以按顺序查看从1到N的数字的索引,并在每一步中计算最小值和最大值的索引。

  • 如果maximum_ind – minimum_ind + 1 = K ,则我们有一个大小为K的排列,否则就没有。
  • 在每个步骤中更新minimum_ind和maximum_ind的值。

时间复杂度: O(n)
插图:

下面是上述方法的实现:

C++
// C++ program to implement
// the above approach
  
#include 
#include 
#include 
using namespace std;
  
int find_permutations(vector& arr)
{
    int cnt = 0;
    int max_ind = -1, min_ind = 10000000;
    int n = arr.size();
    unordered_map index_of;
  
    // Save index of numbers of the array
    for (int i = 0; i < n; i++) {
        index_of[arr[i]] = i + 1;
    }
  
    for (int i = 1; i <= n; i++) {
  
        // Update min and max index
        // with the current index
        // and check if it's a valid permutation
        max_ind = max(max_ind, index_of[i]);
        min_ind = min(min_ind, index_of[i]);
        if (max_ind - min_ind + 1 == i)
            cnt++;
    }
  
    return cnt;
}
  
// Driver code
int main()
{
    vector nums;
    nums.push_back(2);
    nums.push_back(3);
    nums.push_back(1);
    nums.push_back(5);
    nums.push_back(4);
  
    cout << find_permutations(nums);
  
    return 0;
}


Java
// Java program to implement 
// the above approach 
import java.util.*;
  
class GFG{
      
public static int find_permutations(
    Vector arr) 
{ 
    int cnt = 0; 
    int max_ind = -1, min_ind = 10000000; 
    int n = arr.size(); 
      
    HashMap index_of = new HashMap<>(); 
              
    // Save index of numbers of the array 
    for(int i = 0; i < n; i++) 
    { 
        index_of.put(arr.get(i), i + 1);
    } 
  
    for(int i = 1; i <= n; i++) 
    { 
          
        // Update min and max index with 
        // the current index and check 
        // if it's a valid permutation 
        max_ind = Math.max(max_ind, index_of.get(i)); 
        min_ind = Math.min(min_ind, index_of.get(i)); 
          
        if (max_ind - min_ind + 1 == i) 
            cnt++; 
    } 
    return cnt; 
} 
  
// Driver Code
public static void main(String[] args)
{
    Vector nums = new Vector(); 
    nums.add(2); 
    nums.add(3); 
    nums.add(1); 
    nums.add(5); 
    nums.add(4); 
      
    System.out.print(find_permutations(nums));
}
}
  
// This code is contributed by divyeshrabadiya07


Python3
# Python3 program to implement
# the above approach
def find_permutations(arr):
      
    cnt = 0
    max_ind = -1
    min_ind = 10000000;
      
    n = len(arr)
    index_of = {}
  
    # Save index of numbers of the array
    for i in range(n):
        index_of[arr[i]] = i + 1
  
    for i in range(1, n + 1):
  
        # Update min and max index with the
        # current index and check if it's a
        # valid permutation
        max_ind = max(max_ind, index_of[i])
        min_ind = min(min_ind, index_of[i])
          
        if (max_ind - min_ind + 1 == i):
            cnt += 1
              
    return cnt
  
# Driver code
if __name__ == "__main__":
  
    nums = []
    nums.append(2)
    nums.append(3)
    nums.append(1)
    nums.append(5)
    nums.append(4)
  
    print(find_permutations(nums))
  
# This code is contributed by chitranayal


C#
// C# program to implement
// the above approach
using System; 
using System.Collections; 
using System.Collections.Generic; 
  
class GFG{
      
static int find_permutations(ArrayList arr)
{
    int cnt = 0;
    int max_ind = -1, min_ind = 10000000;
    int n = arr.Count;
      
    Dictionary index_of = new Dictionary(); 
              
    // Save index of numbers of the array
    for(int i = 0; i < n; i++) 
    {
        index_of[(int)arr[i]] = i + 1;
    }
  
    for(int i = 1; i <= n; i++)
    {
          
        // Update min and max index with 
        // the current index and check 
        // if it's a valid permutation
        max_ind = Math.Max(max_ind, index_of[i]);
        min_ind = Math.Min(min_ind, index_of[i]);
          
        if (max_ind - min_ind + 1 == i)
            cnt++;
    }
    return cnt;
}
  
// Driver Code
public static void Main(string[] args)
{
    ArrayList nums = new ArrayList(); 
  
    nums.Add(2);
    nums.Add(3);
    nums.Add(1);
    nums.Add(5);
    nums.Add(4);
  
    Console.Write(find_permutations(nums));
}
}
  
// This code is contributed by rutvik_56


输出:
3