SALESFORCE Coding Question – Solved
In Salesforce's annual hackathon, employees from a team are ranked based on their efficiency scores. These scores are represented as an array. The team lead wants to analyze subgroups of employees to identify groups with specific characteristics. Given: · n employees in a team standing in a line, · An array efficiency representing each employee's efficiency, and · An integer k (1 ≤ k ≤ n), representing the reference employee in the array.
Find the number of subarrays of odd lengths with a median equal to efficiency[k].
Note: · A subarray is a sequence of consecutive elements of the array. · The median of an array of odd length, say n, is the (n + 1)/2th element of the array if sorted in non-decreasing order. For example, the median of [2, 5, 4, 1, 1, 1, 6] of length 7 is 2, since upon sorting, the array becomes [1, 1, 1, 2, 4, 5, 6] and the (7 + 1)/2 = 4th element is 2.
Example: efficiency = [5, 3, 1, 4, 7, 7], k = 4 efficiency[4] = 4. There are 4 odd length subarrays with 4 as their median: [4], [1, 4, 7], [5, 3, 1, 4, 7], [3, 1, 4, 7, 7]. Return 4.
Function Description: Complete the function getSpecialSubarrays in the editor below.
getSpecialSubarrays has the following parameters: int efficiency[n]: efficiencies of people int k: the index of the required median value Returns long: the number of odd-length subarrays where arr[k] is the median
Constraints: · The value at efficiency[k] occurs only once in the array. · 1 ≤ n ≤ 3 * 10^5 · 1 ≤ efficiency[i] ≤ 10^9 · 1 ≤ k ≤ n
Input Format for Custom Testing
Sample Case 0
Sample Input 0 STDIN 3 1 2
FUNCTION
n = 3 efficiency = [1, 2, 3] 2 1
Sample Output 0 k = 1 1
Explanation: The only subarray of odd length with 1 as its median is [1].
Asked in:
SALESFORCE