My issue
how
for
1
1 2 3
answer will be 5
My code
#include <bits/stdc++.h>
using namespace std;
using ll=long long int;
int main() {
// your code goes here
ll t;
cin>>t;
while(t--)
{
ll n;
cin>>n;
ll arr[n];
for(ll i=0;i<n;i++)
{
cin>>arr[i];
}
unordered_set<ll> st;
ll be=arr[0];
ll mov=be;
st.insert(be);
while(true)
{
if(be==arr[mov-1])
{
break;
}
else
{
mov=arr[mov-1];
st.insert(mov);
}
}
ll ans=0;
for(ll i=1;i<=n;i++)
{
if(st.find(i)==st.end())
{
ans=ans+(i+1);
}
}
cout<<ans<<endl;
}
}
Problem Link: Costly Permutations Practice Coding Problem