Company: Google
Difficulty: medium
Shrink the Array Max. score: 20.00 You are given an array A of size N. Among all the elements in the array having the same value, replace the element which is in the middle position with the sum of all same value elements, and remove all the other same value elements. The relative position of the middle element should remain the same as that in the original array. Among multiple middle positions take the second one. Task Determine the final array after shrinking. Example Assumptions N = 10 A = [1, 2, 2, 1, 1, 2, 1, 3, 3, 3] Approach For value = 1, elements at indexes 1, 4, 5, and 7 are grouped The middle position (second among middle positions) is 4 Sum of all 1\'s is 4 For value = 2, elements at indexes 2, 3, and 6 are grouped The middle position is 3 Sum of all 2\'s is 6 For value = 3, elements at indexes 8, 9, and 10 are grouped The middle position is 9 Sum of all 3\'s is 9 Final Result Output Array: [4, 6, 9] Explanation: Each group of same numbers is replaced by their sum at the m