Company: Oracle_19aug
Difficulty: medium
Element Swapping Problem Description Given an array of n integers, you can perform the following operation any number of times: Choose any index (0 <= i < n - 1) and swap arr[i] and arr[i + 1]. Each element can be swapped at most once during the process. The strength of an index is defined as (arr[i] * (i + 1)), using 0-based indexing. Find the maximum possible sum of the strength of all indices after optimal swaps: n-1 ∑ arr[i] ⋅ (i + 1) i=0 Examples Example 1: n = 4 arr = [2, 1, 4, 3] Optimal swaps: Swap arr[2], arr[3] Swap arr[0], arr[1] Final array: [1, 2, 3, 4] Sum of strengths: (1 * 1) + (2 * 2) + (3 * 3) + (4 * 4) = 30 This is the maximum possible value, so return 30. Sample Case 0: Input: n = 5 arr = [1, 9, 7, 3, 2] Output: 66 Explanation: It is optimal to swap (arr[2], arr[3]). The final array is [1, 9, 3, 7, 2]. The sum of strengths = (1*1 + 9*2 + 3*3 + 7*4 + 2*5) = 66. Sample Case 1: Input: n = 3 arr = [1, 2, 5] Output: 28 Explanation: No swaps are needed