runtime error #PERMUT2

getting runtime on my code

#include <iostream>
using namespace std;
typedef long long ll;

int main() {
	ll n,i,a[100001],s=0;
	
	while(1){
		cin>>n;
		while(n!=0){
		for(i=1;i<=n;i++){
		cin>>a[i];
	}
	for (i=1;i<=n;i++){
		if (a[a[i]]==i){
			s+=1;
		}
	}
	if (s==n){
		cout<<"ambiguous"<<endl;
	}
	else{
		cout<<"non ambiguous"<<endl;
	}
	}
	}
	return 0;
}

You have to end the program as soon as you see that n=0. It is written in the problem statement that the last test case is followed be a zero. You are not doing that. You just wrote while(1) and then your logic. But this makes the program to run endlessly. So, while(n!=0) { do logic } return 0;

1 Like

There is your code with n AC. You did not correct the n=0 condition properly. CodeChef: Practical coding for everyone
It had to break once n=0. But your outer while loop did run continuously because of while(1)

while(1){
cin>>n;
while(n!=0){
i did as whenever 0 is read it breaks
if i m not right plz fix my code and let me kw

i was talking about the outer while loop. it keeps on going continuously. btw, i’ll fix it!

@chashmeetsingh don’t expect others to ‘fix’ your code. Only when you fix it yourself will you learn.

@nisargshah95 yea you r ryt bro, but still fixing it this time. He doesnt seem to understand the problem. So, just this once.

And btw, you can reduce the running time of your code considerably by using a faster i/o method. Just add this line std::ios::sync_with_stdio(false); below the int main line. CodeChef: Practical coding for everyone