How to attempt?
Question
Moving Apples
There are N number of baskets, where the ith basket contains input2[1]
apples
We want to move apples between baskets so that all baskets have the same
number of apples. What is the minimum number of apples that must be
moved?
it is guaranteed that there exists a way to move apples so as to have an
equal number of apples in each basket.
a
Input Specification:
input1: N, denoting the Number of Baskets
input2: array representing the number of apples in the ith basket
Output Specification:
Return the minimum number of apples that must be moved so
Answers
Explanation:
find the average of the number of apples
and run a for loop for the array
and if avg>arr[i], store it in diff =diff+abs(avg-arr[i])
return diff
that's is your ans
Final answer:
Program
// C++ implementation of above approach
#include <bits/stdc++.h>
using namespace std;
// Function that will calculate the probability
int Number(int First, int Second, int Third)
{
return (Second + Third) - First + 1;
}
// Driver code
int main()
{
int First = 3, Second = 5, Third = 3;
cout << "Maximum apple kept is = "<< Number(First, Second, Third);
return 0;
}
Explanation:
- Example 1;
input1: 2
input2: {1, 3}
Output: 1
First consider there are 2 baskets with first containing 1 apple and second contain 3 apples. If we shift 1 apple from second container to the first container then both will have 2 apples. So only one apple is required to move.
Hence, the output is 1.
- Example 2;
input1: 5
input2: {2849, 1620, 705, 1, 30}
Output: 2387
Now consider there are 5 baskets containing 2849, 1620, 705, 1 and 30 apples respectively. If we move 336 apples from 1st container to 3rd container, 1040 apples from the 1st container to 4rth container, 432 apples from the 1st container and 579 apples from the 2nd container to 5th container, then each container will have 1041 apples. So the total apple movement is,
336 + 1040 + 432 + 579 = 2387
Hence, the output is 2387
To know more about the concept please go through the links;
https://brainly.in/question/39635310
https://brainly.in/question/734888
#SPJ3