Skip to content

ORIGINAL CODING QUESTION

Portfolio endorsement index

Each of your n portfolio projects has an endorsement count. Your index is the largest h such that at least h projects each have h or more endorsements. Return the index; it is 0 when no project is endorsed.

Open practice workspace

Original exercise · medium. Editorial role relevance does not imply a company has asked this question.

Input and output

First integer n, then n integers. Print one integer.

Constraints

1 ≤ n ≤ 200; 0 ≤ endorsements ≤ 1000.

Example 1

Input

5
6 1 4 0 5

Output

3

Three projects have at least 3 endorsements, but only two have at least 4.

Example 2

Input

3
0 0 0

Output

0

No project is endorsed.

Explain your approach

Explain your time and auxiliary space complexity. Which input makes your algorithm work hardest?

Private grading inputs, expected outputs and canonical reference implementations are withheld. Authored expected outputs were cross-checked with independent reference oracles; compiler/runtime behavior still needs a verified execution provider.

Hint 1

The index can never exceed n.

Hint 2

Sort the counts from largest to smallest.

Hint 3

Walk the sorted list: while the count at position h (zero-based) is at least h + 1, increase h; the final h is the index.

Practice this question

Save your work privately, explain decisions and return to your revision queue.

Open practice workspaceBrowse the question library →