Open In App

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

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: 
 

Follow the steps below to solve the problem:

 

 

Below is the implementation of the above approach.

 




// 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 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




# 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# 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




<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)


Article Tags :