Skip to content
Related Articles

Related Articles

Minimum sum of values subtracted from array elements to make all array elements equal
  • Last Updated : 23 Dec, 2020

Given an array arr[] consisting of N positive integers, the task is to find the sum of all the array elements required to be subtracted from each array element such that remaining array elements are all equal.

Examples:

Input: arr[] = {1, 2}
Output: 1
Explanation: Subtracting 1 from arr[1] modifies arr[] to {1, 1}. Therefore, the required sum is 1.

Input: arr[] = {1, 2, 3}
Output: 3
Explanation: Subtracting 1 and 2 from arr[1] and arr[2] modifies arr[] to {1, 1, 1}. Therefore, the required sum = 1 + 2 = 3.

Approach: The idea is to reduce all array elements to the minimum element present in the array. Follow the below steps to solve the problem:



Below is the implementation of the above approach:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program for the above approach
#include <bits/stdc++.h>
using namespace std;
 
// Function to find the sum of values
// removed to make all array elements equal
int minValue(int arr[], int n)
{
    // Stores the minimum of the array
    int minimum = *min_element(
        arr, arr + n);
 
    // Stores required sum
    int sum = 0;
 
    // Traverse the array
    for (int i = 0; i < n; i++) {
 
        // Add the value subtracted
        // from the current elemnt
        sum = sum + (arr[i] - minimum);
    }
 
    // Return the total sum
    return sum;
}
 
// Driver Code
int main()
{
    int arr[] = { 1, 2, 3 };
    int N = sizeof(arr) / sizeof(arr[0]);
 
    // Function Call
    cout << minValue(arr, N);
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program for the above approach
import java.util.Arrays;
class GFG
{
     
// Function to find the sum of values
// removed to make all array elements equal
static int minValue(int []arr, int n)
{
    Arrays.sort(arr);
     
    // Stores the minimum of the array
    int minimum = arr[0];
 
    // Stores required sum
    int sum = 0;
 
    // Traverse the array
    for(int i = 0; i < n; i++)
    {
         
        // Add the value subtracted
        // from the current elemnt
        sum = sum + (arr[i] - minimum);
    }
     
    // Return the total sum
    return sum;
}
 
// Driver Code
static public void main(String args[])
{
    int []arr = { 1, 2, 3 };
    int N = arr.length;
     
    // Function Call
    System.out.println(minValue(arr, N));
}
}
 
// This code is contributed by AnkThon

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 program for the above approach
 
# Function to find the sum of values
# removed to make all array elements equal
def minValue(arr, n):
     
    # Stores the minimum of the array
    minimum = min(arr)
 
    # Stores required sum
    sum = 0
 
    # Traverse the array
    for i in range(n):
         
        # Add the value subtracted
        # from the current elemnt
        sum = sum + (arr[i] - minimum)
         
    # Return the total sum
    return sum
 
# Driver Code
if __name__ == '__main__':
     
    arr = [ 1, 2, 3 ]
    N = len(arr)
     
    # Function Call
    print(minValue(arr, N))
 
# This code is contributed by mohit kumar 29

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program for the above approach
using System;
 
class GFG{
     
// Function to find the sum of values
// removed to make all array elements equal
static int minValue(int []arr, int n)
{
    Array.Sort(arr);
     
    // Stores the minimum of the array
    int minimum = arr[0];
 
    // Stores required sum
    int sum = 0;
 
    // Traverse the array
    for(int i = 0; i < n; i++)
    {
         
        // Add the value subtracted
        // from the current elemnt
        sum = sum + (arr[i] - minimum);
    }
     
    // Return the total sum
    return sum;
}
 
// Driver Code
static public void Main ()
{
    int []arr = { 1, 2, 3 };
    int N = arr.Length;
     
    // Function Call
    Console.WriteLine(minValue(arr, N));
}
}
 
// This code is contributed by AnkThon

chevron_right


Output: 

3

 

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

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up
Recommended Articles
Page :