Checks whether an array represents a permutation.

View versions (1)

Interface

#include <codecogs/maths/combinatorics/permutations/check.h>

using namespace Maths::Combinatorics::Permutations;

This function verifies that each of the integers from 1 to n occurs among the entries of the given array, in other words checking if it is a permutation.

Example:

#include <codecogs/maths/combinatorics/permutations/check.h>
#include <iostream>
int main()
{
  int sigma[5] = {1, 3, 2, 3, 4}, tau[4] = {4, 3, 2, 1};
  std::cout << "Sigma is ";
  std::cout << (Maths::Combinatorics::Permutations::check(5, sigma) ? "" : "not ");
  std::cout << "a valid permutation.";
  std::cout << std::endl;
  std::cout << "Tau is ";
  std::cout << (Maths::Combinatorics::Permutations::check(4, tau) ? "" : "not ");
  std::cout << "a valid permutation.";
  std::cout << std::endl;
  return 0;
}

Output:

Sigma is not a valid permutation.
Tau is a valid permutation.

References

SUBSET, a C++ library of combinatorial routines, http://www.csit.fsu.edu/~burkardt/cpp_src/subset/subset.html

Parameters

n
the size of the p array
p
the actual array

Returns

true, if p is a valid permutation, false otherwise
GPL Licence — free for non commercial use. See Licence details.