Why am I getting Wrong answer for Rearranging digits problem? Problem code: DIGARR

#include
using namespace std;
int main()
{
int t,n,f;
char c;
cin>>t;
while(t–) {
cin>>n;
f=0;
for (int i=0; i<n; i++) {
cin>>c;
if (c == ‘0’ || c == ‘5’) {
f=1;
break;
}
}
if (f==1) cout<<“YES\n”;
else cout<<“NO\n”;
}
return 0;
}

Your code does not read the test cases correctly. It should read all the D decimal digits of N before checking the value of each digit. The for-loop in your code stops as soon as digit 0 or 5 is found. So, it does not read all the D digits of N when 0 or 5 is found before the last digit.

Thanks for the help on the scenario.

Because I think in the question it’s been given that number should be in string form not in int. Here is the correct solution:

#include<bits/stdc++.h>
using namespace std;
int main()
{
	int t;
	cin>>t;
	while(t--){
		long long int d;
		string str;
		bool flag = false;
		cin>>d>>str;
		for(int i=0;str[i];i++)
		{
			int x = str[i] - 48;
			if( x==0 or x == 5)
			{
				flag = true;
				break;
			}
		}
		if( flag == true){
			cout<<"Yes"<<endl;
		}else{
			cout<<"No"<<endl;
		}
	}
	return 0;
}
2 Likes