We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
Hi I am trying to solve this by first sorting the elements and consider each element as median ( the number to which we should convert all other number) and trying to get the coins for other elements to convert to the median and finally will display the minimum coins that we get.
First trying to calculate the difference between the median and its right side(elements that are higher than the median) elements. Then add the difference. Getting this sum of difference to caluclate the cost for performing the first operation. Cost will be directly multiply the sum of difference with the value k.
Then getting the sum of difference on the left side. This will be used to calculate to perform second operation. I'll subract the sum of median's right side difference from left side as we already used them for performing the first opertaion. The cost for performing second operation will be remaining difference multiplied by l.
The sum of the above two will give the answer. Similarly will do the steps for all elements as median then will display the minimum value as the cost. What am I missing in this.
int upperMedianDiff = 0;
for (int i = medianIndex + 1; i < n; i++) {
if (elementsList.get(i) != median) {
upperMedianDiff += (elementsList.get(i) - median);
}
}
int lowerMedianDiff = 0;
for (int i = 0; i < medianIndex; i++) {
if (elementsList.get(i) != median) {
lowerMedianDiff += (median - elementsList.get(i));
}
}
if (upperMedianDiff <= lowerMedianDiff) {
tempAns += (upperMedianDiff * k);
tempAns += (lowerMedianDiff - upperMedianDiff) * l;
}
if (tempAns != 0) {
ans = Math.min(ans, tempAns);
}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Beautiful Array
You are viewing a single comment's thread. Return to all comments →
Hi I am trying to solve this by first sorting the elements and consider each element as median ( the number to which we should convert all other number) and trying to get the coins for other elements to convert to the median and finally will display the minimum coins that we get.
First trying to calculate the difference between the median and its right side(elements that are higher than the median) elements. Then add the difference. Getting this sum of difference to caluclate the cost for performing the first operation. Cost will be directly multiply the sum of difference with the value k.
Then getting the sum of difference on the left side. This will be used to calculate to perform second operation. I'll subract the sum of median's right side difference from left side as we already used them for performing the first opertaion. The cost for performing second operation will be remaining difference multiplied by l.
The sum of the above two will give the answer. Similarly will do the steps for all elements as median then will display the minimum value as the cost. What am I missing in this.
int upperMedianDiff = 0; for (int i = medianIndex + 1; i < n; i++) { if (elementsList.get(i) != median) { upperMedianDiff += (elementsList.get(i) - median); } } int lowerMedianDiff = 0; for (int i = 0; i < medianIndex; i++) { if (elementsList.get(i) != median) { lowerMedianDiff += (median - elementsList.get(i)); } } if (upperMedianDiff <= lowerMedianDiff) { tempAns += (upperMedianDiff * k); tempAns += (lowerMedianDiff - upperMedianDiff) * l; } if (tempAns != 0) { ans = Math.min(ans, tempAns); }