Fish Eater

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
CodingNinjas
author
2y
Checking previous fishes

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 st...read more

CodingNinjas
author
2y
Checking largest fish

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 st...read more
Help your peers!
Add answer anonymously...
Amazon Software Developer Intern Interview Questions
Stay ahead in your career. Get AmbitionBox app
qr-code
Helping over 1 Crore job seekers every month in choosing their right fit company
65 L+

Reviews

4 L+

Interviews

4 Cr+

Salaries

1 Cr+

Users/Month

Contribute to help millions
Get AmbitionBox app

Made with ❤️ in India. Trademarks belong to their respective owners. All rights reserved © 2024 Info Edge (India) Ltd.

Follow us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter