给定一个由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] – minimum) 添加到所需的总和。
- 遍历完数组后,打印得到的和。
下面是上述方法的实现:
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 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
输出:
3
时间复杂度: O(N)
辅助空间: O(1)
如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程和学生竞争性编程现场课程。