#include<stdio.h> int main() { int t,i,n,s,z,flag; scanf("%d",&t); while(t--) { scanf("%d",&n); int arr[n]; for(i=0;i<n;i++) { scanf("%d",&arr[i]); if(i==0) s=arr[i]; if(arr[i]<=s) s=arr[i]; } flag=0; for(i=0;i<n;i++) { if(arr[i]%s!=0) flag++; } if(flag==0) { for(i=0;i<n;i++) { arr[i]=arr[i]/s; printf(" %d",arr[i]); } } else { for(i=0;i<n;i++) { printf(" %d",arr[i]); } } printf("\n"); } return 0; }
Asked by: Manish_Kumar_Savita on April 7, 2019, 6:34 p.m. Last updated on April 7, 2019, 6:34 p.m.
In your solution, you checking if all the elements of the array are divisible by the smallest array element or not. Your code will give correct output if GCD (greatest common divisor) of all the array elements is the smallest array itself or 1.
But let's check this test case where GCD is neither the smallest element in the array nor 1.
N = 3
Array = {20, 50, 25}
Your output: 20 50 25
Expected Output: 4 10 5
Also, please print your answer as left alligned.
You are using:
printf(" %d", arr[i]);
Correct way to print the answer should be:
printf("%d ", arr[i]);