Open In App

Minimum numbers to be appended such that mean of Array is equal to 1

Improve
Improve
Like Article
Like
Save
Share
Report

Given an array arr[ ] of size N, the task is find minimum number of operation required to make mean of Array arr[ ] equal to 1. In one operation, a non-negative number can be appended in the end of the array.

Examples:

Input: N = 3, arr = {1, 1, 1}
Output: 0
Explanation:
As it can be seen that mean of arr[ ], (1+1+1)/3 = 1, 
Therefore 0 operations required to make array good.

Input: N = 4, arr = {8, 4, 6, 2}
Output: 16
Explanation:
As the sum of the given array is 20 and number of element are 4. 
Therefore we need to append 16 zero in the last of array, to make its mean equal to 1.

Approach: The above problem can be solved with the help of array sum and count of elements in array, i.e. N, as per below cases: 
 

  • If the array sum is less than N, the difference between them can be appended in the Array, and hence 1 operation is required.
  • If the array sum is equal to N, then the mean will be equal to 1, and hence 0 operations are required.
  • If the array sum is greater than N, then 0 can be appended in the array (arraySum – N) times. Hence (arraySum – N) operations are required.

Follow the steps below to solve the problem:

 

 

Below is the implementation of the above approach.

 

C++




// C++ program for above approach
#include<bits/stdc++.h>
using namespace std;
 
// Function to calculate minimum
// Number of operations
void minumumOperation(int N, int arr[]){
 
// Storing sum of array arr[]
    int sum_arr =  0;
    sum_arr = accumulate(arr, arr+N, sum_arr);
 
    if(sum_arr >= N)
      cout<<sum_arr-N<<endl;
 
    else
        cout<<1<<endl;
}
 
// Driver Code
int main(){
   int N = 4;
   int arr[] = {8, 4, 6, 2};
 
// Function Call
   minumumOperation(N, arr);
}
 
// This code is contributed by ipg2016107.


Java




// Java program for the above approach
import java.io.*;
class GFG
{
 
// Function to calculate minimum
// Number of operations
static void minumumOperation(int N, int arr[])
{
  
// Storing sum of array arr[]
    int sum_arr =  0;
    for(int i = 0; i < N; i++)
    {
        sum_arr += arr[i];
    }
  
    if(sum_arr >= N)
      System.out.println(sum_arr - N);
  
    else
        System.out.println("1");
}
// Driver Code
public static void main(String[] args)
{
     int N = 4;
   int arr[] = {8, 4, 6, 2};
  
// Function Call
   minumumOperation(N, arr);
}
}
 
// This code is contributed by dwivediyash


Python3




# Python program for above approach
 
# Function to calculate minimum
# Number of operations
def minumumOperation(N, arr):
 
    # Storing sum of array arr[]
    sum_arr = sum(arr)
 
    if sum_arr >= N:
        print(sum_arr-N)
 
    else:
        print(1)
 
 
# Driver Code
N = 4
arr = [8, 4, 6, 2]
 
# Function Call
minumumOperation(N, arr)


C#




// C# program for above approach
using System;
class GFG
{
 
  // Function to calculate minimum
  // Number of operations
  static void minumumOperation(int N, int []arr){
 
    // Storing sum of array arr[]
    int sum_arr =  0;
    for (int i = 0; i < N; i++) {
      sum_arr = sum_arr + arr[i];
    }
 
    if(sum_arr >= N)
      Console.Write(sum_arr-N);
 
    else
      Console.Write(1);
  }
 
  // Driver Code
  static public void Main (){
    int N = 4;
    int []arr = {8, 4, 6, 2};
 
    // Function Call
    minumumOperation(N, arr);
  }
}
 
// This code is contributed by shivanisinghss2110


Javascript




<script>
        // JavaScript Program to implement
        // the above approach
 
        // Function to calculate minimum
        // Number of operations
        function minumumOperation(N, arr) {
 
            // Storing sum of array arr[]
            let sum_arr = 0;
            for (let i = 0; i < N; i++) {
                sum_arr = sum_arr + arr[i];
            }
 
            if (sum_arr >= N)
                document.write(sum_arr - N + "<br>");
 
            else
                document.write(1 + "<br>");
        }
 
        // Driver Code
 
        let N = 4;
        let arr = [8, 4, 6, 2];
 
        // Function Call
        minumumOperation(N, arr);
 
// This code is contributed by Potta Lokesh
    </script>


Output

16

Time Complexity: O(N)
Auxiliary Space: O(1)



Last Updated : 02 Sep, 2021
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads