Maximum Beauty by Deletions
Updated
Problem statement
Given an integer array a, delete some of its elements so that the remaining array has the largest possible beauty, and return that beauty.
The beauty of an array b of length m is the number of indices i with 1 <= i <= m and b[i] == i. Indices start at 1, so the first element is b[1].
You may repeat this operation any number of times, including zero: if the current array has length at least 2, choose any element and delete it. The other elements keep their order. So at least one element always remains.
Function
maxBeauty(a: int[]) → intExamples
Example 1
a = [1,9,3]return = 2Keep every element. Then b[1] = 1 and b[3] = 3, so the beauty is 2. The 9 never matches its index, but deleting it would move the 3 to index 2, and [1, 3] has beauty 1. No array can do better, because a beauty of 3 would need the values 1, 2 and 3. So the answer is 2.
Example 2
a = [1,3,2,3]return = 3Kept as it is, only b[1] = 1 matches. Delete the 3 at index 2 to get [1, 2, 3], where every index matches. The answer is 3.
Example 3
a = [4,4,4]return = 0Every value is 4, but any remaining array has at most 3 elements, so no index can hold a 4. Whatever you delete, the beauty is 0.
Constraints
1 <= a.length <= 20001 <= a[i] <= 10^5
More Meesho Online Assessment questions
Similar questions at other companies
- Maximum Score with Prime JumpsMicrosoft · OAMicrosoft interview questions
- Card Game: Best First-Player ScoreShopify · OAShopify interview questions
- Maximum Game Score with Fixed Forward JumpsStripe · OAStripe interview questions
- 0/1 KnapsackConfluent · OAConfluent interview questions
- Minimum Cost to Tile a Strip with Widths 1, 2, and 3Infosys · OAInfosys interview questions
- Maximum Subarray SumStraive · OA
- Longest Increasing Price SubsequenceOpenAI · OAOpenAI interview questions
- Lowest-Cost Shirt Color ScheduleElevenLabs · OAElevenLabs interview questions
- Maximum Game Board ScoreJP Morgan Chase · OAJP Morgan Chase interview questions
- API Bandwidth AllocationArista Networks · OAArista Networks interview questions