Get Maximum Count
Amazon has launched a "Play to Win" game where users get a chance to earn free gift vouchers. The game presents you with an array of integers (arr) and an integer k. You are allowed to choose any contiguous subarray within arr and add an integer x of your choice to all the elements within that subarray. You can do this at most once.
The goal is to maximize the number of elements in the entire array that have a value equal to k after performing this operation (choosing a subarray and adding x to it).
You need to complete the function getMaximumCount which takes the integer array arr and the target value k as input, and returns the maximum number of elements equal to k that can be achieved.
Complete the function getMaximumCount in the editor.
getMaximumCount has the following parameters:
- 1.
int[] arr: an array of integers - 2.
int k: the target value
Returns
int: the maximum number of elements equal to k that can be achieved
1Example 1
If we choose the subarray [4, 3] (from index 3 to 4) and add x = -2 to it, the array becomes [2, 3, 2, 2, 1, 2]. In this new array, there are four elements with the value 2. It's stated that this is the maximal count achievable, so the answer would be 4.
2Example 2
By choosing the subarray from index 1 to 5 (inclusive, assuming 0-based indexing, so the subarray is [4,4,6,4,4]) and adding x=2 to it, the subarray becomes [6,6,8,6,6]. The resulting array would be [6,6,6,8,6,6]. This gives us 5 elements equal to k=6, which is stated to be maximal.
Constraints
Limits and guarantees your solution can rely on.
1≤n≤2 · 10^5(where n is the size of the array arr)1≤arr[i]≤2 · 10^5(for each element in arr)1≤k≤2 · 10^5