给定一个由N个正整数组成的数组arr [] ,任务是找到需要从每个数组元素中减去的所有数组元素的总和,以使其余数组元素都相等。
例子:
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.
方法:想法是将所有数组元素减少到数组中存在的最小元素。请按照以下步骤解决问题:
- 初始化一个变量,例如sum ,以存储所有相减值的和。
- 使用min_element()找到存在于数组中的最小元素,例如minimum 。
- 遍历数组,并为每个数组元素(例如arr [i] )加上(arr [i] –最小值)到所需的总和。
- 遍历数组后,打印获得的总和。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
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;
}
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 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
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 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
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 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
Javascript
输出:
3
时间复杂度: O(N)
辅助空间: O(1)