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 elemnt 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;} |
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 elemnt 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 |
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.
Recommended Posts:
- Value to be subtracted from array elements to make sum of all elements equals K
- Least number to be added to or subtracted from N to make it a Perfect Square
- Least number to be added to or subtracted from N to make it a Perfect Cube
- Number of times the largest perfect square number can be subtracted from N
- Check if there exists a prime number which gives Y after being repeatedly subtracted from X
- Number of times the largest Perfect Cube can be subtracted from N
- Smallest number to be subtracted to convert given number to a palindrome
- Minimum increments to make all array elements equal with sum same as the given array after exactly one removal
- All possible values of floor(N/K) for all values of K
- Minimum swaps of similar indexed elements required to make all elements of one of the two given arrays equal
- Minimum operation to make all elements equal in array
- Minimum number of increment-other operations to make all array elements equal.
- Minimum operations required to make all the array elements equal
- Find the minimum number of operations required to make all array elements equal
- Minimum value of X to make all array elements equal by either decreasing or increasing by X
- Minimum cost to make all array elements equal
- Minimum operations to make all elements equal using the second array
- Minimum Cost to make all array elements equal using given operations
- Minimum number of steps to make all elements of array equal
- Make all array elements equal with minimum cost
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : mohit kumar 29

