Computer Science, asked by harimekala81, 4 days ago

Fish in a Fish Tank There are a number of fishes in a fish tank. Each of the fish has been treated with some amount of food. After each day, if any fish has more food than the fish on its left, it dies being weaker than the left one. You are given with the initial quantity of the food for each of the fish. Resolve the number of days after which no fish dies, i.e. the day after which there is no fish with more ford content than to its left fish. 14 ) 15 Example: 16- class 17 L = [3,6,2,7,5] //Food levels First Day: Fish on index 2 and 4 die,leaving L=[3,2,5]. On the second day fish at index 3 will die. Since there are no fish left with a higher level of food than the left fish, after Day 2 fish in the fish tank will stop dying. Description: Complete the function Fish Tank.food in the editor below. FishTank.food has the following parameter(s): 18 19 20 21​

Answers

Answered by janvisingla81066
0

Answer:

Fish in a Fish Tank There are a number of fishes in a fish tank. Each of the fish has been treated with some amount of food. After each day, if any fish has more food than the fish on its left, it dies being weaker than the left one. You are given with the initial quantity of the food for each of the fish. Resolve the number of days after which no fish dies, i.e. the day after which there is no fish with more ford content than to its left fish. 14 ) 15 Example: 16- class 17 L = [3,6,2,7,5] //Food levels First Day: Fish on index 2 and 4 die,leaving L=[3,2,5]. On the second day fish at index 3 will die. Since there are no fish left with a higher level of food than the left fish, after Day 2 fish in the fish tank will stop dying. Description: Complete the function Fish Tank.food in the editor below. FishTank.food has the following parameter(s): 18 19 20 21

Answered by aditijaink283
0

Answer:

The correct answer to the given question is explained in the explanation.

Explanation:

You are given two non-empty zero-indexed arrays A and B of N integers. Arrays A and B represent N voracious fish of a downstream sorted river.

Fish numbers range from 0 to N - 1. If P and Q are two fish and P < Q, then fish P is initially upstream of fish Q. Initially, each fish has its own unique location.

The number of fish P is expressed as A[P] and B[P]. Array A contains the size of the fish. Every element is unique. Array B contains the orientation of the fish. It contains only 0s and/or 1s. where

0 represents upstream fish and 1 represents downstream fish. If the two fish are moving in opposite directions and there is no other (living) fish in between, they will eventually meet. Then only one fish will survive. Big fish eat small fish. More precisely, two fish P and Q meet when P < Q, B[P] = 1, B[Q] = 0, and there is no living fish between them. After encounter:

If A[P] > A[Q], P eats Q and P still flows downstream. If A[Q] > A[P] then Q will swallow P and Q will still flow upstream. Assume that all fish swim at the same speed.

Code for the given problem is:

public int solution(int[] a, int[] b) {

 int reFish = a.length;

 int i = 0;

 for (i = 0; i < b.length; i++) {

   if(b[i] != 0) {

     /*reFish++; }else { */ break;

   }

 }

 Stack<Integer> myQ = new Stack<Integer>();

 for (int j = i; j < b.length; j++) {

   if(b[j] == 1)

   {

     myQ.add(j);

   }

   while(b[j] == 0 && !myQ.isEmpty()) {

     if(a[j] > a[myQ.peek()]) {

       myQ.pop(); reFish--;

     }else{

       reFish--;

     break;

     }

   }

 }

 return reFish;

}

#SPJ2

Similar questions