can anyone tell what is wrong in my code…
https://www.codechef.com/viewsolution/8710955
plzz tell my miistake in this solution
Try declaring b array globally outside the main function.
There seems to be some problem with your logic here too,
Refer to line 2021
a[c]=1;
c=a[c];
Value of c becomes negative i.e 1
In line 17
while(a[c]!=i)
You are using c as a subscript of an array for comparison.
So here you are actually comparing “while(a[1]!=i)”
In C you cannot use negative subscript for an array(a[1]) until here a represents a pointer pointing to the second or greater element.
Hope this helps.
With the type of code you’ve written, there is little help you will find here except if you post your pseudocode. I just wrote a solution for the problem, and I think you might be able to understand it.
Ask any questions in the comments.
Pseudocode:

Mark all indices as unvisited

Find the lowest unvisited index i and store it in x (for future use)

Visit the number at index i, and set i=a[i]

Repeat 3 while a[i]!=x

Goto 2, Exit if all indices visited.
Code:
#include <bits/stdc++.h>
using namespace std;
struct _ { ios_base::Init i; _() { cin.sync_with_stdio(0); cin.tie(0); } } _;
///////////////////////////////////////////////////////////////////////////////
#define endl '\n'
#define ll long long
#define ld long double
#define pb push_back
#define mt make_tuple
#define in(a) for (auto& i: a)
#define tbeg clock_t _t=clock();
#define test ll t; cin >> t; for (ll tc=1; tc<=t; tc++) {
#define tend cout << "\n\nTime: " << (double)(clock()_t)/CLOCKS_PER_SEC;
///////////////////////////////////////////////////////////////////////////////
ll pickindex(ll visited[], ll n)
{
ll i;
for (i=1; i<=n && visited[i]; i++);
return i>n ? 0 : i;
}
int main()
{
ll n, i, x, ctr=0;
cin >> n;
ll a[n+1], visited[n+1];
in(visited) i=0;
for (i=1; i<=n; i++) cin >> a[i];
stringstream buffer;
for (i=pickindex(visited, n); i; ctr++, i=pickindex(visited, n))
{
visited[i]=1;
buffer << i << ' ';
for (ll x=i; a[i]!=x; visited[a[i]]=1, i=a[i]) buffer << a[i] << ' ';
buffer << a[i] << endl;
}
cout << ctr << endl << buffer.str();
}
i have corrected my mistake it ran correct for the testcases but it is now showing wrong ans… https://www.codechef.com/viewsolution/8802018