Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Problem Statement Rabbit Hanako has N = carrots.size() boxes of carrots numbered

ID: 3786924 • Letter: P

Question

Problem Statement

Rabbit Hanako has N = carrots.size() boxes of carrots numbered 0 through N-1. The ith box contains carrots[i]carrots. (Note that carrots is the name of a Vector that contains information about boxes of the vegetable named carrot.)

She decides to eat amount carrots from these boxes. She will eat the carrots one at a time, each time choosing a carrot from the box with the greatest number of carrots. If there are multiple such boxes, she will choose the lowest numbered box among them.

Return the number of the box from which she will eat her last carrot.

Constraints

carrots will contain between 1 and 50 elements, inclusive.

Each element of carrots will be between 1 and 100, inclusive

amount will be between 1 and the sum of all elements of carrots, inclusive

My Code:

int last_box(Vector &carrots, int amount) {
// fill in code here
int boxNum = 0;
int largeBox = carrots[carrots.size() - 1];

for (int j = amount; j >0; j--) {
for (int i = (carrots.size() - 1); i >= 0; i--) {
if (carrots[i] > carrots[i - 1]) {
largeBox = i;
}
}
carrots[largeBox] = carrots[largeBox] - 1;
boxNum = largeBox;
}
return boxNum;
}

The purpose is to: Return the number of the box from which she will eat her last carrot.

Explanation / Answer

class CarrotBoxes{
public static void main(String [] args){
int Index(vector <int> carrots, int A) {

   int tmp=0;
   for(int j=0 ; j<A ; j++){
       int tmpmax=0;
       for(int n=0 ; n<carrots.size() ; n++){
           if(carrots[n] > tmpmax){
               tmpmax = carrots[n];
               tmp = n;
           }
       }
       carrots[tmp]--;
   }

   return tmp;
}
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote