I am running this in Dev C++ it is giving me Id returned 1 exit status. What did i do wrong here?
A permutation of length n is an array consisting of n distinct integers from 1 to n in arbitrary order. For example, [2,3,1,5,4] is a permutation, but [1,2,2] is not a permutation (2 appears twice in the array) and [1,3,4] is also not a permutation (n=3 but there is 4 in the array).
Let p be any permutation of length n. We define the fingerprint F(p) of p as the sorted array of sums of adjacent elements in p. More formally,
F(p)=sort([p1+p2,p2+p3,…,pn−1+pn]). For example, if n=4 and p=[1,4,2,3], then the fingerprint is given by F(p)=sort([1+4,4+2,2+3])=sort([5,6,5])=[5,5,6].
You are given a permutation p of length n. Your task is to find a different permutation p′ with the same fingerprint. Two permutations p and p′ are considered different if there is some index i such that pi≠p′i.
#include <bits/stdc++.h>
using namespace std;
int* fingerprint(int p[],int n)
{
int fp[n-1];
for(int i=0;i<n-1;i++)
{
fp[i]=p[i]+p[i+1];
}
sort(fp,fp+n-1);
return fp;
}
void display(int p[],int n)
{
for(int i=0;i<n;i++)
cout<<p[i]<<" ";
cout<<endl;
}
void equality(int p[],int* fp,int n)
{
int* fp1=fingerprint(p,n);
int c=0;
for(int i=0;i<n-1;i++)
{
if(fp[i]!=fp1[i])
c=1;
}
if(c==0)
display(p,n);
}
void findpermutations(int p[],int n)
{
int* fp=fingerprint(p,n);
sort(p,p+n);
do{
equality(p,fp,n);
}while(next_permutation(p,p+n));
}
int main()
{
int t;
cin>>t;
while(t--)
{
int n;
cin>>n;
int p[n];
for(int i=0;i<n;i++)
{
cin>>p[i];
}
findpermutations(p,n);
}
return 0;
}
Input:
3
2
1 2
6
2 1 6 5 4 3
5
2 4 3 1 5
Output:
2 1
1 2 5 6 3 4
3 1 5 2 4