Find Maximum Greatness
Updated
Problem statement
Given an array arr of length n, you can rearrange its elements to form another array, called rearranged_arr. The greatness of a rearrangement is the number of indices i, 0 ≤ i < n, where rearranged_arr[i] > arr[i]. That is, the element placed at index i after rearranging is strictly greater than the original value at that index.
Return the maximum possible greatness over all rearrangements of arr.
Complete the function findMaximumGreatness in the editor.
findMaximumGreatness has the following parameter:
int arr[n]: an array of integers
The function returns an int: the maximum possible greatness.
Function
findMaximumGreatness(arr: int[]) → intExamples
Example 1
arr = [1, 3, 5, 2, 1, 3, 1]return = 4Original arr = [1, 3, 5, 2, 1, 3, 1]. One optimal rearrangement is rearranged_arr = [2, 5, 1, 3, 3, 1, 1].
At indices 0, 1, 3 and 4 the new value is greater: 2 > 1, 5 > 3, 3 > 2 and 3 > 1. The three 1s in rearranged_arr can never be greater than the value at their index, because every value is at least 1. So at least 3 indices lose, and the greatness is at most 7 - 3 = 4. The answer is 4.
Constraints
1 ≤ n ≤ 1051 ≤ arr[i] ≤ 109
More Twilio interview questions
Similar questions at other companies
- Catch Fish with Reusable BaitsSusquehanna International Group (SIG) · OASusquehanna International Group (SIG) interview questions
- Can Every Package Fit in a BoxIMC Trading · OAIMC Trading interview questions
- Minimum Tunnel Crossing TimeArcesium · OAArcesium interview questions
- Get Maximum PairsAmazon · OA
- Binge WatchingVanguard · OAVanguard interview questions
- Maximum Easy Tasks After RearrangingIBM · OAIBM interview questions
- Latest Arrival to Catch a BusAutodesk · OAAutodesk interview questions
- Minimum Removals to Balance ArraySalesforce · OASalesforce interview questions
- Minimum Machines for TasksExpedia · OAExpedia interview questions
- Find Minimum Possible SizeMathWorks · OAMathWorks interview questions