Optimizing Resource Allocation
Updated
Problem statement
You are given an array arr of n resources and a number k. A subarray is a group of items that sit next to each other in arr.
Pick a subarray that follows both rules:
- It has exactly
kitems. - All of its items are different.
Return the largest sum such a subarray can have. If no subarray follows both rules, return -1.
For example, in [1, 2, 3, 7, 3, 5] with k = 3, the subarray [7, 3, 5] has three different items and the largest sum, 15.
Function
findOptimalResources(arr: int[], k: int) → intExamples
Example 1
arr = [1, 2, 3, 7, 3, 5]k = 3return = 15These subarrays of length k = 3 have three different items:
[1, 2, 3]: 1 + 2 + 3 = 6[2, 3, 7]: 2 + 3 + 7 = 12[7, 3, 5]: 7 + 3 + 5 = 15
[3, 7, 3] does not count, because 3 appears twice. The largest sum is 15.
Constraints
1 ≤ k ≤ arr.length ≤ 10^51 ≤ arr[i] ≤ 10^4
More Dell Technologies interview questions
Similar questions at other companies
- Maximize Resource AllocationIBM · OAIBM interview questions
- API Rate Limit Violation DetectionTekion · OATekion interview questions
- Count Belt Sections With K Fruit Pairs (Forward Deployed Engineer)Boston Consulting Group · OABoston Consulting Group interview questions
- Incident MonitorStripe · OAStripe interview questions
- Count Subarrays with at Least K Equal-Fruit PairsTikTok · OATikTok interview questions
- Consistent LogsRippling · OARippling interview questions
- Minimum Length Good SubarrayMicrosoft · OAMicrosoft interview questions
- Auction Sniping Window MinimumCisco · OACisco interview questions
- Balanced Prefix Sets in a PermutationUber · OAUber interview questions
- Shortest Subarray Covering an Integer RangeMarshall Wace · OAMarshall Wace interview questions