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
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;
}
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();
}
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