给定一个包含N 个正整数的数组arr ,任务是检查给定的数组 arr 是否表示一个排列。
A sequence of N integers is called a permutation if it contains all integers from 1 to N exactly once.
例子:
Input: arr[] = {1, 2, 5, 3, 2}
Output: No
Explanation:
The given array is not a permutation of numbers from 1 to N, because it contains 2 twice, and 4 is missing for the array to represent a permutation of length 5.
Input: arr[] = {1, 2, 5, 3, 4}
Output: Yes
Explanation:
Given array contains all integers from 1 to 5 exactly once. Hence, it represents a permutation of length 5.
朴素的方法:显然,给定的数组将仅表示长度为 N 的排列,其中 N 是数组的长度。所以我们必须在给定的数组中搜索从 1 到 N 的每个元素。如果找到所有元素,则数组表示排列,否则不表示排列。
时间复杂度: O(N 2 )
有效的方法:
上述方法可以使用一组数据结构进行优化。
- 遍历给定数组并插入集合数据结构中的每个元素。
- 另外,找到数组中的最大元素。这个最大元素将是值 N,它代表集合的大小。
- 遍历数组后,检查集合的大小是否等于 N。
- 如果集合的大小等于 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
如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程和学生竞争性编程现场课程。