Panda Guru LogoPanda
Guru

Salesforce OA || 24 sept

Round 1

Questions: Given an array arr that contains n integers, the following operation can be performed on it any number of times (possibly zero):

The strength of an index i is defined as (arr[i] + 1) * (i + 1) using 0-based indexing. Find the maximum possible sum of the strength of all indices after optimal swaps. Mathematically, maximize the following:

Sum = Σ (arr[i] * (i + 1))

Example: Consider n = 4, arr = [2, 1, 4, 3]. It is optimal to swap arr[2] and arr[3] and arr[0] and arr[1]. The final array is [1, 2, 3, 4]. The sum of strengths is 1*1 + 2*2 + 3*3 + 4*4 = 30, which is the maximum possible. Thus, the answer is 30.

Function Description: Complete the function getMaximumSumOfStrengths in the editor below.

getMaximumSumOfStrengths has the following parameter:

Testcases:

Candidate's Approach

The candidate was able to solve the problem partially using a greedy approach. They focused on identifying optimal swaps that would maximize the sum of strengths based on the defined formula.

Interviewer's Feedback

No feedback provided.