Last Updated: 11 Jan, 2021

Fish Eater

Easy
Asked in companies
OlaAmazon

Problem statement

There is a river which flows in one direction. One day, the river has 'N' number of fishes. You are given an array 'FISHES' of integers representing the size of 'N' fishes. The fishes are present in the river from left to right(0th index represents the size of the leftmost fish). As the river flows from left to right so the fishes also swim from left to right. The fishes are of different sizes and have different speeds. The larger fishes are faster than the smaller fishes. Also, larger fishes can eat smaller fishes but can’t eat fishes of the same size.

Can you find the number of fishes that survive at the end of the day?

Example:
Let the size of fishes present in the river from left to right be{ 4, 2, 3, 1, 5 }.

1. As fish 4 is faster and bigger than fish 2, so fish 4 will eat fish 2.
    Remaining fishes: { 4, 3, 1, 5 }

2. As fish 3 is faster and bigger than fish 1, so fish 3 will eat fish 1.
    Remaining fishes: { 4, 3, 5 }

3. As fish 4 is faster and bigger than fish 3, so fish 4 will eat fish 3.
    Remaining fishes: { 4, 5 }

Now fish 5 cannot eat fish 4, as fish 5 is faster than fish 4 and they swim from left to right. Thus, fish 4 will never reach fish 5.

Finally, we are left with only 2 fishes.
Input format:
The first line of input contains an integer ‘T’ denoting the number of test cases.

The first line of each test case contains an integer ‘N’ denoting the number of fishes.

The second line of each test case contains 'N' single space-separated integers representing the size of the fishes.
Output format :
For each test case, return the number of fishes that survives till last.
Note:
You don't need to print anything, it has already been taken care of. Just implement the given function.
Constraints:
1 <= T <= 10
1 <= N <= 10^4
1 <= FISHES[i] <= 10^9

Where 'FISHS[i]' denotes the size of the ith fish in the river.

Time limit: 1 sec

Approaches

01 Approach

The first approach would be to simply check if a fish will be alive at the end or not. We can find this just by checking if a fish has any bigger fish behind it or not.

 

The steps are as follows:

 

  • Traverse over each fish.
  • For each fish check if it has any bigger fish behind it.
  • If it does not have any bigger fish behind it just increase the count of remaining fishes.
  • Return the count of remaining fishes.

02 Approach

All we need to find if a fish is alive or not by checking if it is bigger than the largest fish behind it or not.

 

The steps are as follows :

 

  • Take a variable, say ‘MAXSIZE’, and store the size of the largest fish found up to now.
  • Traverse over each fish.
  • For each fish check if its size is greater than ‘MAXSIZE’.
    • If it is larger than the ‘MAXSIZE’, update the size of the largest fish to the size of the current fish and increase the count of remaining fishes.