通过最大化所选元素的总和将给定的 Array 减少为 0
给定一个包含N个正整数的数组arr[] ,任务是在每次求和运算后所有剩余数组元素都减 1 时使数组和最大化。
注意:任何数组元素的值都不会低于 0。
例子:
Input: arr[] = {6, 2, 4, 5}
Output: 12
Explanation: Add 6 initially to the final sum.
The final sum becomes 6 and the remaining array elements {1, 3, 4}.
Add 3 with the sum. The sum becomes 9 and the remaining elements {0, 3}
Add 3 with the sum. The sum becomes 12 and only 0 remains in the array.
Add 0 with the sum. The sum remains unchanged.
Input: arr[] = {5, 6, 4}
Output: 12
天真的方法:从数组中找到最大元素。将其添加到总和中,并将所有其他元素减 1,并将当前元素更改为 0,这样它就不会在循环中再次重复。执行此过程,直到所有元素都变为 0。
C++
// C++ code to implement the above approach
#include
using namespace std;
// Find maximum possible sum
int maxSum(int arr[], int N)
{
// Initialize ans with 0
int ans = 0;
// loop till atleast one element is greater than 0
while (1) {
// maximum element of array
int maxValueIndex = max_element(arr, arr + N) - arr;
// breaking condition when all elements become <=0
if (arr[maxValueIndex] <= 0)
break;
// adding value to answer
ans += arr[maxValueIndex];
arr[maxValueIndex] = 0;
// Iterate array
for (int i = 0; i < N; i++) {
arr[i]--;
}
}
return ans;
}
// Driver code
int main()
{
// Given array of values
int arr[] = { 6, 2, 4, 5 };
int N = sizeof(arr) / sizeof(arr[0]);
// Function call
cout << maxSum(arr, N);
return 0;
}
Java
// Java code to implement the above approach
import java.util.*;
class GFG
{
// Find maximum possible sum
static int maxSum(int arr[], int N)
{
// Initialize ans with 0
int ans = 0;
// loop till atleast one element is greater than 0
while (true) {
// maximum element of array
int maxValue = Arrays.stream(arr).max().getAsInt();
;
int maxValueIndex = 0;
for (int i = 0; i < arr.length; i++) {
if (arr[i] == maxValue) {
maxValueIndex = i;
break;
}
}
// breaking condition when all elements become <=0
if (arr[maxValueIndex] <= 0)
break;
// adding value to answer
ans += arr[maxValueIndex];
arr[maxValueIndex] = 0;
// Iterate array
for (int i = 0; i < N; i++) {
arr[i]--;
}
}
return ans;
}
// Driver code
public static void main(String[] args)
{
// Given array of values
int arr[] = { 6, 2, 4, 5 };
int N = arr.length;
// Function call
System.out.print(maxSum(arr, N));
}
}
// This code is contributed by gauravrajput1
Python3
# Python code to implement the above approach
# Find maximum possible sum
def maxSum(arr, N):
# Initialize ans with 0
ans = 0
# loop till atleast one element is greater than 0
while (1):
# maximum element of array
maxValueIndex = arr.index(max(arr))
# breaking condition when all elements become <=0
if (arr[maxValueIndex] <= 0):
break
# adding value to answer
ans += arr[maxValueIndex]
arr[maxValueIndex] = 0
# Iterate array
for i in range(0, N):
arr[i] -= 1
return ans
# Driver code
# Given array of values
arr = [6, 2, 4, 5]
N = len(arr)
# Function call
print(maxSum(arr, N))
# This code is contributed by ninja_hattori.
Javascript
C++
// C++ code to implement the above approach
#include
using namespace std;
// Find maximum possible sum
int maxSum(int arr[], int N)
{
// Initialize ans with 0
int ans = 0;
// Sort array in descending order
sort(arr, arr + N, greater());
// Iterate array
for (int i = 0; i < N; i++) {
// Starting value
int value = arr[i];
// Actual value when being added
int current = max(0, value - i);
// Add actual value with ans
ans = ans + current;
}
return ans;
}
// Driver code
int main()
{
// Given array of values
int arr[] = { 6, 2, 4, 5 };
int N = sizeof(arr) / sizeof(arr[0]);
// Function call
cout << maxSum(arr, N);
return 0;
}
Java
// Java code to implement the above approach
import java.util.*;
public class GFG
{
// Utility program to reverse an array
public static void reverse(int[] array)
{
// Length of the array
int n = array.length;
// Swaping the first half elements with last half
// elements
for (int i = 0; i < n / 2; i++) {
// Storing the first half elements temporarily
int temp = array[i];
// Assigning the first half to the last half
array[i] = array[n - i - 1];
// Assigning the last half to the first half
array[n - i - 1] = temp;
}
}
// Find maximum possible sum
static int maxSum(int[] arr, int N)
{
// Initialize ans with 0
int ans = 0;
// Sorting the array in ascending order
Arrays.sort(arr);
// Reversing the array
reverse(arr);
// Iterate array
for (int i = 0; i < N; i++) {
// Starting value
int value = arr[i];
// Actual value when being added
int current = Math.max(0, value - i);
// Add actual value with ans
ans = ans + current;
}
return ans;
}
// Driver code
public static void main(String args[])
{
// Given array of values
int[] arr = { 6, 2, 4, 5 };
int N = arr.length;
// Function call
System.out.println(maxSum(arr, N));
}
}
// This code is contributed by Samim Hossain Mondal.
Python
# Python code to implement the above approach
# Find maximum possible sum
def maxSum(arr, N):
# Initialize ans with 0
ans = 0
# Sort array in descending order
arr.sort(reverse = True)
# Iterate array
for i in range(N):
#Starting value
value = arr[i]
# Actual value when being added
current = max(0, value - i)
# Add actual value with ans
ans = ans + current
return ans
# Driver code
if __name__ == "__main__":
# Given array of values
arr = [ 6, 2, 4, 5 ]
N = len(arr)
# Function call
print(maxSum(arr, N))
# This code is contributed by hrithikgarg03188.
C#
// C# code to implement the above approach
using System;
class GFG
{
// Find maximum possible sum
static int maxSum(int[] arr, int N)
{
// Initialize ans with 0
int ans = 0;
// Sort array in descending order
Array.Sort(
arr, delegate(int m, int n) { return n - m; });
// Iterate array
for (int i = 0; i < N; i++) {
// Starting value
int value = arr[i];
// Actual value when being added
int current = Math.Max(0, value - i);
// Add actual value with ans
ans = ans + current;
}
return ans;
}
// Driver code
public static int Main()
{
// Given array of values
int[] arr = { 6, 2, 4, 5 };
int N = arr.Length;
// Function call
Console.Write(maxSum(arr, N));
return 0;
}
}
// This code is contributed by Taranpreet
Javascript
时间复杂度: O(N 2 )
辅助空间: O(1)
高效的方法:问题的解决是基于排序的概念。随着每一步中值的减少,应首先将较高的值与最终总和相加。请按照以下步骤解决问题:
- 按降序对数组进行排序。
- 运行从1 到 N的循环。
- 对于每个索引i ,添加到最终总和的值将是(arr[i] – i) ,因为它将在i减少值之后添加。
- 由于总和值不能低于0 ,因此将max(arr[i]-i, 0)添加到最终总和中。
- 返回最终总和。
下面是上述方法的实现。
C++
// C++ code to implement the above approach
#include
using namespace std;
// Find maximum possible sum
int maxSum(int arr[], int N)
{
// Initialize ans with 0
int ans = 0;
// Sort array in descending order
sort(arr, arr + N, greater());
// Iterate array
for (int i = 0; i < N; i++) {
// Starting value
int value = arr[i];
// Actual value when being added
int current = max(0, value - i);
// Add actual value with ans
ans = ans + current;
}
return ans;
}
// Driver code
int main()
{
// Given array of values
int arr[] = { 6, 2, 4, 5 };
int N = sizeof(arr) / sizeof(arr[0]);
// Function call
cout << maxSum(arr, N);
return 0;
}
Java
// Java code to implement the above approach
import java.util.*;
public class GFG
{
// Utility program to reverse an array
public static void reverse(int[] array)
{
// Length of the array
int n = array.length;
// Swaping the first half elements with last half
// elements
for (int i = 0; i < n / 2; i++) {
// Storing the first half elements temporarily
int temp = array[i];
// Assigning the first half to the last half
array[i] = array[n - i - 1];
// Assigning the last half to the first half
array[n - i - 1] = temp;
}
}
// Find maximum possible sum
static int maxSum(int[] arr, int N)
{
// Initialize ans with 0
int ans = 0;
// Sorting the array in ascending order
Arrays.sort(arr);
// Reversing the array
reverse(arr);
// Iterate array
for (int i = 0; i < N; i++) {
// Starting value
int value = arr[i];
// Actual value when being added
int current = Math.max(0, value - i);
// Add actual value with ans
ans = ans + current;
}
return ans;
}
// Driver code
public static void main(String args[])
{
// Given array of values
int[] arr = { 6, 2, 4, 5 };
int N = arr.length;
// Function call
System.out.println(maxSum(arr, N));
}
}
// This code is contributed by Samim Hossain Mondal.
Python
# Python code to implement the above approach
# Find maximum possible sum
def maxSum(arr, N):
# Initialize ans with 0
ans = 0
# Sort array in descending order
arr.sort(reverse = True)
# Iterate array
for i in range(N):
#Starting value
value = arr[i]
# Actual value when being added
current = max(0, value - i)
# Add actual value with ans
ans = ans + current
return ans
# Driver code
if __name__ == "__main__":
# Given array of values
arr = [ 6, 2, 4, 5 ]
N = len(arr)
# Function call
print(maxSum(arr, N))
# This code is contributed by hrithikgarg03188.
C#
// C# code to implement the above approach
using System;
class GFG
{
// Find maximum possible sum
static int maxSum(int[] arr, int N)
{
// Initialize ans with 0
int ans = 0;
// Sort array in descending order
Array.Sort(
arr, delegate(int m, int n) { return n - m; });
// Iterate array
for (int i = 0; i < N; i++) {
// Starting value
int value = arr[i];
// Actual value when being added
int current = Math.Max(0, value - i);
// Add actual value with ans
ans = ans + current;
}
return ans;
}
// Driver code
public static int Main()
{
// Given array of values
int[] arr = { 6, 2, 4, 5 };
int N = arr.Length;
// Function call
Console.Write(maxSum(arr, N));
return 0;
}
}
// This code is contributed by Taranpreet
Javascript
12
时间复杂度: O(N log N)
辅助空间: O(1)