Wednesday 1 August 2012

Given a number and its permutation, how can we find out the number of transformations by which the number could be transformed into its permutation ? eg: 2315 and 5213 are given. 2315 can be transformed into 5213 in a series of 2 transformations.


#include<iostream.h>
#include<conio.h>
int a1[]={2,3,1,5};
int a2[]={5,2,1,3};
int counter,n=4;
void swap(int i,int j)
{
      counter++;
      cout<<"\nswap "<<a2[i]<<" and "<<a2n[j];
      int temp=a2[i];
      a2[i]=a2[j];
      a2[j]=temp;
}
void main()
{
      clrscr();
      for(int i=0;i<n;i++)
            if(a1[i]!=a2[i])
            {
                  int pos=0;
                  while(a2[pos]!=a1[i])
                        pos++;
                  swap(pos,i);
            }
      cout<<"\n\nno of operations required = "<<counter;
      getch();
}

No comments:

Post a Comment