RECIPE - Editorial

Why i am getting WA(wrong answer) ??

I am getting right answers for all the TEST cases, incuding (8,16,20) ans=(2,4,5).Given in comment.
Can anyone help me.

My solution link
https://www.codechef.com/viewsolution/16701304

@grayhathacker

#include<stdio.h>
int main()
{
int t,n,i,j,k,m,a[1000],s[1000],gcd;
scanf("%d",&t);
if(t<=100)
{
for(i=1;i<=t;i++)
{
scanf("%d",&n);
if(n>=2&&n<=50)
{
for(j=0;j<n;j++)
{
scanf("%d",&a[j]);
}
}
gcd=a[0];
for(k=1;k<n;k++)
{
if(a[k]%gcd==0)
{
k++;
}
else{
gcd=a[k]%gcd;
}
}
for(m=0;m<n;m++)
{
s[m]=a[m]/gcd;
printf("%d \n",s[m]);
}

}

}
return 0;
}

please tell me what is wrong with my code it’s working on my system but when I submit it show me wrong answer

#include<stdio.h>
int main()
{
int t,n,i,j,k,m,a[1000],s[1000],gcd;
scanf("%d",&t);
if(t<=100)
{
for(i=1;i<=t;i++)
{
scanf("%d",&n);
if(n>=2&&n<=50)
{
for(j=0;j<n;j++)
{
scanf("%d",&a[j]);
}
}
gcd=a[0];
for(k=1;k<n;k++)
{
if(a[k]%gcd==0)
{
k++;
}
else{
gcd=a[k]%gcd;
}
}
for(m=0;m<n;m++)
{
s[m]=a[m]/gcd;
printf("%d \n",s[m]);
}

}

}
return 0;
}

please tell me what is wrong with my code it’s working on my system but when I submit it show me wrong answer

#include
using namespace std;
int main(){
int n;
cin>>n;
for(int i=0;i<n;i++){
int q,c=0;
cin>>q;
int a[q];
for(int i=0;i<q;i++)
{
cin>>a[i];
}
int m=a[i];
for(int i=0;i<q;i++)
{
if(a[i]<m)
{
m=a[i];
}
}for(int j=m;j>0;j–)
{c=0;
for(int i=0;i<q;i++)
{
if(a[i]%j==0)
{
c++;
}
if(c==q)
{ for(int i=0;i<q;i++)
cout<<a[i]/j<<" ";
}

}
if(c==q){
    cout<<endl;
    break;
}
}

}
}
what is wrong in this my compiler is giving right answer?

step 1: just find highest common factor(HCF) of all the nos

for ex:In the given question for
case 1: no(s) are :4 ,4 => hcf = 4
case 2: 2,3,4 => hcf will be 1
case 3: 3 15 9 6 => hcf will be 3

step 2:
divide each no of each case with hcf

for ex:
case 1: 4,4 => hcf=4 therefore new nos will be 1,1
case 2: 2,3,4 => hcf=1 therefore new nos will be 2,3,4
case 3: 3,15,9,6 => hcf=3 therefore new nos will be 1,5,3,2

you can see code of this question for given link:
https://www.codechef.com/viewsolution/23428055

it’s not necessary that the no. with minimum value will always be gcd of all…

try this : 8 16 20

ur output: 8 16 20

correct answer : 2 4 5

2 Likes

thank you!

Can you help me with this!
I am getting
Output as 1 1 11 for your test case

#include <bits/stdc++.h>
using namespace std;

long int gcd (long int a, long int b)
{
    if (a == 0)
    return b;
    gcd(b%a, a);
}
int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int t;
    cin >> t;
    long int num;
    while (t--)
    {
      cin >> num;
      long int q[num];
      long int sum =0;
      for(int i=0; i<num; i++){
      cin >> q[i];
      sum = sum+ q[i];
      }
      for(int i=0; i<num; i++){
      cout<<q[i]/gcd(q[i],sum)<<" ";
      
      }
      cout << "\n";
      
      
      
    }
    return 0;
}

https://www.codechef.com/viewsolution/33370255
I dont know what is wrong in this ,i have divide all numbers by smallest number among them and i am getting the required answer ?

Here’s a naive solution to the problem

#include <bits/stdc++.h> 

#define ll long long int
#define ln "\n"
#define fast ios::sync_with_stdio(0); cin.tie(nullptr);

using namespace std;


int main(){
fast;

int t; 
cin >> t;

while(t--){
    int n;
    cin >> n;
    int arr[n];
    for(int i=0; i<n; ++i)
        cin >> arr[i];
    int count=0, mn=*min_element(arr, arr+n);
    for(auto i: arr){
        if(i%mn==0){
            count++;
        }
    }
    int temp = mn-1;
    int newcount=0;

    if(count != n){
        while (temp > 0)
        {
            for(auto i: arr){
                if(i%temp==0){
                    newcount++;
                }
            }
            if(newcount==n){
                break;
            }
            else{
                temp--;
                newcount=0;
            }
        }
    }
                
        
    if(count == n){
        for(auto j : arr){
            cout << j/mn << " ";
        }
        
    }
    else if(newcount == n){
        for(auto j : arr){
            cout << j/temp << " ";
        }
    }
    else{
        for(auto j : arr){
            cout << j << " ";
        }
    }
    cout << ln;
}
return 0;
}

whats wrong in this code?
https://www.codechef.com/viewsolution/36912471

What is the problem in this java code. I am getting all the answers right but after submitting I got “Wrong Answer” but why ?
// your code goes here
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
for(int i=0;i<t;i++){
int n=sc.nextInt();
int[] arr=new int[n];
int[] original=new int[n];
for(int j=0;j<n;j++){
arr[j]=sc.nextInt();
original[j]=arr[j];
}
Arrays.sort(arr);
int mini=arr[0];
boolean possible=true;
for(int j=0;j<n;j++){
if(arr[j]%mini!=0){
possible=false;
break;
}
}
if(!possible){
for(int j=0;j<n;j++){
System.out.print(original[j]+" “);
}
System.out.println();
}
else{
for(int j=0;j<n;j++){
System.out.print(original[j]/mini+” ");
}
System.out.println();
}

    	}

What is wrong with my code ?
https://www.codechef.com/viewsolution/45489236

Output:
50 63 56 57 38 82 105 32 91

404 cannot be reduced to 50

I just solved this problem using gcd algorithm

But after reading the editorial i found that we can just check number from 1-1000 and this would be fast.

Just amazaing.

input is given in the number of ingredients and quantity of ingredients

and output is asked in the least number of quantity of ingredients, or
determine how much of each ingredient to use in order to make as little food as possible.

how is this sounding like GCD ?

why cant the solution be 1 for every ingredient? it is the least right

why am I getting as wrong answer ? what is the mistake , can anyone please guide me?

Assuming you mean:

https://www.codechef.com/viewsolution/53786091

then consider the test input:

1
1 93

Also, first paragraph of this :stuck_out_tongue: Getting wrong answer on submission but test cases are running fine (ZCO12001) - #3 by ssjgz