Minimum sum of values subtracted from array elements to make all array elements equal Last Updated : 08 Jun, 2021 Comments Improve Suggest changes Like Article Like Report 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: 1Explanation: Subtracting 1 from arr[1] modifies arr[] to {1, 1}. Therefore, the required sum is 1. Input: arr[] = {1, 2, 3}Output: 3Explanation: 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 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 element 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; } Java // 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 element 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 Python3 # 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 element 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 C# // 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 element 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 JavaScript <script> // Javascript program for the above approach // Function to find the sum of values // removed to make all array elements equal function 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> Output: 3 Time Complexity: O(N)Auxiliary Space: O(1) Comment More infoAdvertise with us Next Article Minimum sum of values subtracted from array elements to make all array elements equal S sallagondaavinashreddy7 Follow Improve Article Tags : Greedy Mathematical DSA Arrays array-rearrange +1 More Practice Tags : ArraysGreedyMathematical Similar Reads Make all array elements equal by reducing array elements to half minimum number of times Given an array arr[] consisting of N integers, the task is to minimize the number of operations required to make all array elements equal by converting Ai to Ai / 2. in each operation Examples: Input: arr[] = {3, 1, 1, 3}Output: 2Explanation: Reducing A0 to A0 / 2 modifies arr[] to {1, 1, 1, 3}. Red 6 min read Minimum increments to modify array such that value of any array element can be splitted to make all remaining elements equal Given an array arr[] consisting of N elements, the task is to find the minimum number of increments required to be performed on the given array such that after selecting any array element at any index and splitting its value to the other array elements makes all other N - 1 elements equal. Examples: 6 min read Minimum value of X to make all array elements equal by either decreasing or increasing by X Given an array of N elements and you can perform two operations on it: Increase any of the array element by X once.Decrease any of the array element by X once. The task is to find the minimum most value of X such that all array elements are equal by either applying operation 1, 2 or not applying any 7 min read Minimum increment and decrement by K of each pair elements required to make all array elements equal Given an array arr[], the task is to check if it is possible to make all array elements equal by repeatedly choosing a triplet (i, j, k), where i and j are different, and subtract k from arr[i] and add k to arr[j]. Examples: Input: arr[] = {1, 5, 6, 4}Output: YesExplanation:Operations performed: Cho 10 min read Minimum delete operations to make all elements of array same Given an array of n elements such that elements may repeat. We can delete any number of elements from the array. The task is to find a minimum number of elements to be deleted from the array to make it equal.Examples: Input: arr[] = {4, 3, 4, 4, 2, 4} Output: 2 After deleting 2 and 3 from array, arr 10 min read Minimum Cost to make all array elements equal using given operations Given an array arr[] of positive integers and three integers A, R, M, where The cost of adding 1 to an element of the array is A,the cost of subtracting 1 from an element of the array is R andthe cost of adding 1 to an element and subtracting 1 from another element simultaneously is M. The task is t 12 min read Minimum changes required to make all element in an array equal Given an array of length N, the task is to find minimum operation required to make all elements in the array equal. Operation is as follows: Replace the value of one element of the array by one of its adjacent elements. Examples: Input: N = 4, arr[] = {2, 3, 3, 4} Output: 2 Explanation: Replace 2 an 5 min read Minimize cost of insertions and deletions required to make all array elements equal Given a sorted array arr[] of size N (1 ? N ? 105) and two integers A and B, the task is to calculate the minimum cost required to make all array elements equal by increments or decrements. The cost of each increment and decrement are A and B respectively. Examples: Input: arr[] = { 2, 5, 6, 9, 10, 11 min read Minimum operation to make all elements equal in array Given an array consisting of n positive integers, the task is to find the minimum number of operations to make all elements equal. In each operation, we can perform addition, multiplication, subtraction, or division with any number and an array element. Examples: Input : arr[] = [1, 2, 3, 4]Output : 11 min read Like