Fish Eater Problem Statement

In a river where water flows from left to right, there is a sequence of 'N' fishes each having different sizes and speeds. The sizes of these fishes are provided in an array called FISHES. Larger fishes swim faster and can eat smaller fishes they encounter. However, they cannot eat fishes of the same size. Determine how many fishes survive at the end of such encounters.

Input:

The input consists of multiple test cases. The first line contains an integer 'T' representing the number of test cases. Each test case consists of two lines. The first line has the integer 'N', the number of fishes. The second line has 'N' space-separated integers specifying the sizes of the fishes.

Output:

For every test case, return the number of fishes that survive.

Example:

Input: 
N = 5
FISHES = [4, 2, 3, 1, 5]
Output:
2
Explanation:
Fish 4 will eat fish 2, leaving [4, 3, 1, 5].
Fish 3 will eat fish 1, leaving [4, 3, 5].
Fish 4 will eat fish 3, leaving [4, 5]. Since fish 5 cannot encounter fish 4, they both survive.

Constraints:

  • 1 <= T <= 10
  • 1 <= N <= 10^4
  • 1 <= FISHES[i] <= 10^9
Note:

You do not need to print the result, the function should only compute and return the output.

AnswerBot
4mo

Given a river with fishes of different sizes and speeds, determine how many fishes survive after encounters.

  • Iterate through the array of fishes and simulate encounters between fishes to determine surv...read more

Saikumarreddy Palakolanu
4mo
public int function(int[] arr){ int count=0; int i=0; int j=1; while(j<arr.length){ if(arr[i]<arr[j]){ count++; i=j; } j++; } return count; }
Help your peers!
Select
Add answer anonymously...

Amazon Software Developer Intern interview questions & answers

A Software Developer Intern was asked 4mo agoQ. Given a tree, find its diameter (the longest path between two nodes in the tree)...read more
A Software Developer Intern was asked 4mo agoQ. Given a matrix, find the shortest distance between two given points located anyw...read more
A Software Developer Intern was asked 4mo agoQ. For a given array, how would you count the number of inversions?

Popular interview questions of Software Developer Intern

A Software Developer Intern was asked 4mo agoQ1. Given a tree, find its diameter (the longest path between two nodes in the tree)...read more
A Software Developer Intern was asked 4mo agoQ2. Given a matrix, find the shortest distance between two given points located anyw...read more
A Software Developer Intern was asked 4mo agoQ3. For a given array, how would you count the number of inversions?
Amazon Software Developer Intern Interview Questions
Stay ahead in your career. Get AmbitionBox app
play-icon
play-icon
qr-code
Trusted by over 1.5 Crore job seekers to find their right fit company
80 L+

Reviews

10L+

Interviews

4 Cr+

Salaries

1.5 Cr+

Users

Contribute to help millions

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

Follow Us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter
Profile Image
Hello, Guest
AmbitionBox Employee Choice Awards 2025
Winners announced!
awards-icon
Contribute to help millions!
Write a review
Write a review
Share interview
Share interview
Contribute salary
Contribute salary
Add office photos
Add office photos
Add office benefits
Add office benefits