Ide:-https://ide.geeksforgeeks.org/mrqwUw57bG
Ques:-https://practice.geeksforgeeks.org/problems/fractional-knapsack-1587115620/1
Fractional knapsack getting test case error
hello @manikjindal49
pls check ur updated code here->
bool mycmp(Item a,Item b){
double r1=((double)a.value/(double)a.weight);//typecast both num and den
double r2=((double)b.value/(double)b.weight);
return r1>r2;
}
double fractionalKnapsack(int W, Item arr[], int n)
{
sort(arr,arr+n,mycmp);
double res=0.0;
int i;
for(i=0;i<n;i++){
if(arr[i].weight<=W){
res=res+arr[i].value;
W=W-arr[i].weight;
}
else{
res=res+(double)arr[i].value*((double) W/(double) arr[i].weight);
break;
}
}
return res;
}
Can you please explain this line return r1>r2; i know here comparison is occuring but i didot understand this line code
well if we want to order array in increasing order then we use < in comparator
and to order array in descending order we use > operator for comparision.
here the array will be ordered in descending order of value/weight
ok sorry i misinterpret the code as it is showing this (>) not the comparison sign thats why i asked