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:
- Initialize a variable, say sum, to store the sum of all the values subtracted.
- Find the smallest element present in the array using min_element(), say minimum.
- Traverse the array and for each array element, say arr[i], add (arr[i] – minimum) to the required sum.
- After complete traversal of the array, print the obtained sum.
Below is the implementation of the above approach:
C++
// 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 equalint 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 element        sum = sum + (arr[i] - minimum);    }Â
    // Return the total sum    return sum;}Â
// Driver Codeint main(){Â Â Â Â int arr[] = { 1, 2, 3 };Â Â Â Â int N = sizeof(arr) / sizeof(arr[0]);Â
    // Function Call    cout << minValue(arr, N);    return 0;} |
Java
// Java program for the above approachimport java.util.Arrays;class GFG{     // Function to find the sum of values// removed to make all array elements equalstatic 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 element        sum = sum + (arr[i] - minimum);    }         // Return the total sum    return sum;}Â
// Driver Codestatic 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 |
Python3
# Python3 program for the above approachÂ
# Function to find the sum of values# removed to make all array elements equaldef 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 element        sum = sum + (arr[i] - minimum)             # Return the total sum    return sumÂ
# Driver Codeif __name__ == '__main__':         arr = [ 1, 2, 3 ]    N = len(arr)         # Function Call    print(minValue(arr, N))Â
# This code is contributed by mohit kumar 29 |
C#
// C# program for the above approachusing System;Â
class GFG{     // Function to find the sum of values// removed to make all array elements equalstatic 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 element        sum = sum + (arr[i] - minimum);    }         // Return the total sum    return sum;}Â
// Driver Codestatic 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 |
Javascript
<script>Â
// Javascript program for the above approachÂ
// Function to find the sum of values// removed to make all array elements equalfunction minValue(arr, n){    // Stores the minimum of the array    var minimum = Math.min.apply(Math,arr);Â
    // Stores required sum    var sum = 0;         var i;    // Traverse the array    for (i = 0; i < n; i++) {Â
        // Add the value subtracted        // from the current element        sum = sum + (arr[i] - minimum);    }Â
    // Return the total sum    return sum;}Â
// Driver CodeÂ
 var arr = [1, 2, 3]; var N = arr.length;Â
// Function Call document.write(minValue(arr, N));Â
</script> |
3
Â
Time Complexity: O(N)
Auxiliary Space: O(1)
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!

… [Trackback]
[…] Read More on on that Topic: geeksforgeeks.org/minimum-sum-of-values-subtracted-from-array-elements-to-make-all-array-elements-equal/ […]
… [Trackback]
[…] Here you will find 26125 additional Information on that Topic: geeksforgeeks.org/minimum-sum-of-values-subtracted-from-array-elements-to-make-all-array-elements-equal/ […]