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.

Saikumarreddy Palakolanu
10d
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!
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

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