I have a task. I know this task is really easy but..
A non-empty zero-indexed array A consisting of N integers is given. A permutation is a sequence containing each element from 1 to N once, and only once. For example, array A such that:
- A[0] = 4
- A[1] = 1
- A[2] = 3
- A[3] = 2
is a permutation, but array A such that:
- A[0] = 4
- A[1] = 1
- A[2] = 3
is not a permutation. The goal is to check whether array A is a permutation.
I implemented this solution, but I think this isn't the best solution.
def solution(A):
# write your code in Python 2.6
maxN = max(A)
B = list(xrange(1,maxN+1))
if sorted(A) == sorted(B):
return 1
else:
return 0
Do you have any ideas how I should solve this problem?