test cases are working properly but still wrong answer
#include
using namespace std;
int main()
{
int n,t,i,j,count=0,min;
int a[50];
cin>>t;
while(t>0)
{
cin>>n;
for(i=0;i<n;i++)
cin>>a[i];
min=a[0];
for(i=0;i<n;i++)
{
if(a[i]<min)
min=a[i];
}
count=0;
for(i=0;i<n;i++)
{
if(a[i]%min==0)
count++;
}
if(count==n)
{
for(i=0;i<n;i++)
cout<<a[i]/min<<" ";
}
else
for(i=0;i<n;i++)
cout<<a[i]<<" ";
cout<<endl;
t–;
}
return 0;
}
All test cases are not working.
Try the case,
1
3 4 6 8
The answer according to your solution is 4 6 8, which is wrong.
The correct output for this test case is 2 3 4
The reason your logic is giving correct answer for all sample test cases is that the gcd of all sample cases is either the minimum number or 1.
Try a different logic.
If you have any problem comment below.
2 Likes
what is wrong with my code
#include <stdio.h>
int main(void) {
int a[60],i,j,n,t,min,temp,flag;
scanf("%d",&t);
while(t–)
{ scanf("%d",&n);
flag=0;
for(i=0;i<n;i++)
scanf("%d",&a[i]);
min=a[0];
for(i=1;i<n;i++)
{ if(min>a[i])
min=a[i];
}
temp=0;
for(i=0;i<n;i++)
if(min==a[i])
temp++;
if(temp==n)
{ flag=1;
for(i=0;i<n;i++)
printf(“1 “);
}
else
{ for(i=2;i<=min;i++)
{ temp=0;
for(j=0;j<n;j++)
{ if(a[j]%i==0)
temp++;
}
if(temp==n)
{ flag=1;
for(j=0;j<n;j++)
printf(”%d “,a[j]/i);
}
}
}
if(flag==0)
for(i=0;i<n;i++)
printf(”%d “,a[i]);
printf(”\n”);
}
return 0;
}