Help in Permutation P[i]=P[P[i]]

Revision en1, by R.A.N.K.A., 2020-10-05 09:32:19

Problem statement — Permutation is called stable if P[i]=i for every i

We are given Permutation we want to tell in how many moves we can make this permutation stable or print -1 if not possible to make permutation stable.

in one move we can do operation : — P[i] = P[P[i]]

Sample TestCase 
7
[1 3 2 5 6 7 4]
Output  - > 2

[1 3 2 5 6 7 4] -> [1 2 3 6 7 4 5] -> [1 2 3 4 5 6 7]

Sample TestCase 
7
[2 3 1 5 6 7 4]
Output  - > -1

Please help and thanks in advance.

History

 
 
 
 
Revisions
 
 
  Rev. Lang. By When Δ Comment
en1 English R.A.N.K.A. 2020-10-05 09:32:19 548 Initial revision (published)