Computer Science, asked by varshithareddy2925, 5 hours ago

Problem Statement
[:
There are n piles of stones. Each pile of stone has a
height Ai. Mickey and Minnie are bored, so they
decide to play a game. The game is as follows -
Minnie will give Mickey an integer k. Now Mickey has
to make the difference between the height of the
highest pile and the lowest equal to k.
In a move, Mickey can either increase the height of
any pile by one or decrease by one.
Find the minimum number of moves Mickey will
need.
Note: Answers can be large use long long.​

Answers

Answered by kunalkale012
1

Answer:

#include <iostream>

using namespace std;

int main()

{

   long long int n;

   cin>>n;

   long long int arr[n];

   long long int i, j, min, temp;

   for(int i=0;i<n;i++)

   {

       cin>>arr[i];

   }

  for (i = 0; i < n - 1; i++) {

     min = i;

     for (j = i + 1; j < n; j++)

     if (arr[j] < arr[min])

     min = j;

     temp = arr[i];

     arr[i] = arr[min];

     arr[min] = temp;

  }

 

  cout<<arr[n-1]-arr[0];

   return 0;

}

Explanation:

In this question we have to just find difference between height of pile of stones so first we sort the array and find difference between minimum and maximum array value

Answered by bhargabnath691
1

Answer:

Explanation:

#include <bits/stdc++.h>

using namespace std;

int main() {

unordered_map<int, int> freq;

int n, k;

cin >> n >> k;

int a[n];

for(int i=0; i<n; i++){

 cin >> a[i];

}

for(int i=0; i<n; i++){

 freq[a[i]]++;

}

int* mx = max_element(a, a+n);

int* mn = min_element(a, a+n);

int ans = 0;

       bool allSame = false;

       if(*mx == *mn) allSame = true;

while(*mx - *mn != k && !allSame){

 int count_max = freq[*mx];

 int count_min = freq[*mn];

 

 if(count_max >= count_min){

  ans += count_max;

  *mn = *mn + 1;

 } else {

  ans += count_min;

  *mx = *mx - 1;

 }

}

cout << ans;

return 0;

}

Similar questions