📌  相关文章
📜  检查数组是否是从 1 到 N 的数字排列

📅  最后修改于: 2021-09-03 03:46:54             🧑  作者: Mango

给定一个包含N 个正整数的数组arr ,任务是检查给定的数组 arr 是否表示一个排列。

例子:

朴素的方法:显然,给定的数组将仅表示长度为 N 的排列,其中 N 是数组的长度。所以我们必须在给定的数组中搜索从 1 到 N 的每个元素。如果找到所有元素,则数组表示排列,否则不表示排列。
时间复杂度: O(N 2 )
有效的方法:
上述方法可以使用一组数据结构进行优化。

  1. 遍历给定数组并插入集合数据结构中的每个元素。
  2. 另外,找到数组中的最大元素。这个最大元素将是值 N,它代表集合的大小。
  3. 遍历数组后,检查集合的大小是否等于 N。
  4. 如果集合的大小等于 N,则数组表示排列,否则不表示排列。

下面是上述方法的实现:

C++
// C++ Program to decide if an
// array represents a permutation or not
 
#include 
using namespace std;
 
// Function to check if an
// array represents a permutation or not
bool permutation(int arr[], int n)
{
    // Set to check the count
    // of non-repeating elements
    set hash;
 
    int maxEle = 0;
 
    for (int i = 0; i < n; i++) {
 
        // Insert all elements in the set
        hash.insert(arr[i]);
 
        // Calculating the max element
        maxEle = max(maxEle, arr[i]);
    }
 
    if (maxEle != n)
        return false;
 
    // Check if set size is equal to n
    if (hash.size() == n)
        return true;
 
    return false;
}
 
// Driver code
int main()
{
    int arr[] = { 1, 2, 5, 3, 2 };
    int n = sizeof(arr) / sizeof(int);
 
    if (permutation(arr, n))
        cout << "Yes" << endl;
    else
        cout << "No" << endl;
 
    return 0;
}


Java
// Java Program to decide if an
// array represents a permutation or not
import java.util.*;
 
class GFG{
 
// Function to check if an
// array represents a permutation or not
static boolean permutation(int []arr, int n)
{
    // Set to check the count
    // of non-repeating elements
    Set hash = new HashSet();
 
    int maxEle = 0;
 
    for (int i = 0; i < n; i++) {
 
        // Insert all elements in the set
        hash.add(arr[i]);
 
        // Calculating the max element
        maxEle = Math.max(maxEle, arr[i]);
    }
 
    if (maxEle != n)
        return false;
 
    // Check if set size is equal to n
    if (hash.size() == n)
        return true;
 
    return false;
}
 
// Driver code
public static void main(String args[])
{
    int arr[] = { 1, 2, 5, 3, 2 };
    int n = arr.length;
 
    if (permutation(arr, n))
        System.out.println("Yes");
    else
        System.out.println("No");
}
}
 
// This code is contributed by Surendra_Gangwar


Python3
# Python3 Program to decide if an
# array represents a permutation or not
 
# Function to check if an
# array represents a permutation or not
def permutation(arr, n):
     
        # Set to check the count
    # of non-repeating elements
    s = set()
 
    maxEle = 0;
 
    for i in range(n):
   
        # Insert all elements in the set
        s.add(arr[i]);
 
        # Calculating the max element
        maxEle = max(maxEle, arr[i]);
     
    if (maxEle != n):
        return False
 
    # Check if set size is equal to n
    if (len(s) == n):
        return True;
 
    return False;
 
# Driver code
if __name__=='__main__':
 
    arr = [ 1, 2, 5, 3, 2 ]
    n = len(arr)
 
    if (permutation(arr, n)):
        print("Yes")
    else:
        print("No")
 
# This code is contributed by Princi Singh


C#
// C# Program to decide if an
// array represents a permutation or not
using System;
using System.Collections.Generic;
 
class GFG{
  
// Function to check if an
// array represents a permutation or not
static bool permutation(int []arr, int n)
{
    // Set to check the count
    // of non-repeating elements
    HashSet hash = new HashSet();
  
    int maxEle = 0;
  
    for (int i = 0; i < n; i++) {
  
        // Insert all elements in the set
        hash.Add(arr[i]);
  
        // Calculating the max element
        maxEle = Math.Max(maxEle, arr[i]);
    }
  
    if (maxEle != n)
        return false;
  
    // Check if set size is equal to n
    if (hash.Count == n)
        return true;
  
    return false;
}
  
// Driver code
public static void Main(String []args)
{
    int []arr = { 1, 2, 5, 3, 2 };
    int n = arr.Length;
  
    if (permutation(arr, n))
        Console.WriteLine("Yes");
    else
        Console.WriteLine("No");
}
}
 
// This code is contributed by Princi Singh


Javascript


输出:
No

如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live