Amazon OA
Anonymous User
265

Q1. Data analysts at Amazon are analyzing the information gained when a model is trained with different arrangements of the same data. For an array of n integers, data, an arrangement is represented using a permutation of the integers from 1 to n. They observed that the information gained for some permutation p of n integers for the array data is equal to the sum of i * data[p[i]]. For example, if data = [2, 4, 5, 3] and p=[2,1,3,4], then the information gained is 1 * data[2] + 2 * data[1] + 3 * data[3] + 4 * data[4] = 1 * 4 + 2 * 2 + 3 * 5 + 4 * 3 = 4 + 4 + 15 + 12 = 35.

Given the array data, find the lexicographically smallest permutation p such that the information gained for the given data is maximum.

Note: A permutation p is considered lexicographically smaller than a permutation q, if the first index i where p[i] != q[i], p[i] < q[i].

Q2. Developers at Amazon are working on a new sorting algorithm for points on the x-axis of the coordinate system.

There are n points. The ith point initially has a weight of weight[i] and is located at position i on the x-axis. In a single position, the ith point can be moved to the right by a distance of dist[i].

Given weight and dist, find the minimum number of operations required to sort the points by their weights.

Function Description

Complete the function getMinOperations2 in the editor -

GetMinOperations2 has the following arguments -

int weights[n]: the weights of the points
int dist[n]: the distances the points can move
Returns

long int: the min num of operations to sort the points. Here long int represents a 64 bit integer.

I bombed Q1, was able to solve Q2.

Comments (1)