using namespace std;
int main()
{
int n,t=1,pos=1;
cin>>n;
int A[n+1];int P[n+1];
int C[n+1];int B[n+1];
for(int i=1;i<=n;i++)
{cin>>A[i];
C[i]=0;P[i]=0;
}
for(int k=1;k<=n;++k)
{
if(C[k]==0)
{
P[pos++]=k; C[k]=1;
for(int i=A[k];i<=n;i=A[i])
{
B[t++]=i;
C[i]=1;
if(i==k) break;
}
}
}
cout<<pos-1<<endl;
int a=1,j=1;
while(a<pos)
{
cout<<P[a];
for(;j<=n;++j)
{
cout<<B[j];
if(P[a]==B[j])
{
j++;
cout<<endl;
break;
}
}
a++;
}
return 0;
}
for each cycle you are getting set of numbers to be printed and at the same time that value are separated by a space …so while printing the number use cout<<" ";
as for example
Sample input 1:
8
2 4 5 1 7 6 3 8
Sample output 1:
4
1 2 4 1
3 5 7 3
6 6
8 8
but your output is coming like this .
4
1241
3573
66
88
1 Like
oh…Thank you so much…