给定大小为N的数组arr [] ,任务是找到数组元素的最大可能和,以便可以根据以下条件选择元素:
- 对于每个索引i ,元素的值为arr [i] ,那么我们可以将1到min(N,arr [i])的任何元素添加到总和中,否则保留该元素。
- 如果某个元素已经添加到总和中,那么我们将无法再次将同一元素添加到该总和中。
例子:
Input: arr[] = {1, 2, 2, 4}
Output: 7
Explanation:
Initially, the total sum is 0.
Sum after adding element at index 1 to sum = 1. Elements added to sum = {1}
Sum after adding element at index 2 to sum = 3. Elements added to sum = {1, 2}
Now, coming at index 3 we can not add any element from (1 to 2) to the sum because we have already added the elements {1, 2} into the sum. So, leave the element at index 3.
Then add the element at index 4 to sum since 4 was not added to sum before therefore the maximum sum we can get is 3+4=7.
Input: arr[] = {4, 4, 1, 5}
Output: 13
方法:可以使用贪婪技术解决此问题。步骤如下:
- 以升序对数组的元素进行排序。
- 保持可以添加到总和的最大可能数(例如maxPossible) 。
- 用数组的最大元素初始化maxPossible并将其添加到总和中。
- 从头到尾遍历数组,然后检查给定元素值是否已添加到总和中。
- 如果之前已添加元素值,则将总和增加(element – 1) ,如果元素值小于maxPossible ,则将其添加并将maxPossible更改为元素值。
- 完成上述步骤后,打印总和的值。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
#define ll long long
using namespace std;
// Function that finds the maximum sum
// of the array elements according to
// the given condition
ll findMaxValue(int arr[], int n)
{
// Sort the array
sort(arr, arr + n);
// Take the max value from the array
ll ans = arr[n - 1];
ll maxPossible = arr[n - 1];
// Iterate from the end of the array
for (int i = n - 2; i >= 0; --i) {
if (maxPossible > 0) {
// Check for the number had
// come before or not
if (arr[i] >= maxPossible) {
// Take the value which is
// not occured already
ans += (maxPossible - 1);
maxPossible = maxPossible - 1;
}
else {
// Change max to new value
maxPossible = arr[i];
ans += maxPossible;
}
}
}
// Return the maximum sum
return ans;
}
// Driver Code
int main()
{
// Given array arr[]
int arr[] = { 4, 4, 1, 5 };
int n = sizeof(arr) / sizeof(arr[0]);
// Function Call
cout << findMaxValue(arr, n);
}
Java
// Java program for the above approach
import java.util.*;
class GFG{
// Function that finds the maximum sum
// of the array elements according to
// the given condition
static int findMaxValue(int arr[], int n)
{
// Sort the array
Arrays.sort(arr);
// Take the max value from the array
int ans = arr[n - 1];
int maxPossible = arr[n - 1];
// Iterate from the end of the array
for (int i = n - 2; i >= 0; --i)
{
if (maxPossible > 0)
{
// Check for the number had
// come before or not
if (arr[i] >= maxPossible)
{
// Take the value which is
// not occured already
ans += (maxPossible - 1);
maxPossible = maxPossible - 1;
}
else
{
// Change max to new value
maxPossible = arr[i];
ans += maxPossible;
}
}
}
// Return the maximum sum
return ans;
}
// Driver Code
public static void main(String[] args)
{
// Given array arr[]
int arr[] = { 4, 4, 1, 5 };
int n = arr.length;
// Function Call
System.out.print(findMaxValue(arr, n));
}
}
// This code is contributed by sapnasingh4991
Python3
# Python3 program for the above approach
# Function that finds the maximum sum
# of the array elements according to
# the given condition
def findMaxValue(arr, n):
# Sort the array
arr.sort()
# Take the max value from the array
ans = arr[n - 1]
maxPossible = arr[n - 1]
# Iterate from the end of the array
for i in range(n - 2, -1, -1):
if (maxPossible > 0):
# Check for the number had
# come before or not
if (arr[i] >= maxPossible):
# Take the value which is
# not occured already
ans += (maxPossible - 1)
maxPossible = maxPossible - 1
else:
# Change max to new value
maxPossible = arr[i]
ans += maxPossible
# Return the maximum sum
return ans
# Driver code
if __name__=='__main__':
# Given array arr[]
arr = [ 4, 4, 1, 5 ]
n = len(arr)
# Function call
print(findMaxValue(arr,n))
# This code is contributed by rutvik_56
C#
// C# program for the above approach
using System;
class GFG{
// Function that finds the maximum sum
// of the array elements according to
// the given condition
static int findMaxValue(int []arr, int n)
{
// Sort the array
Array.Sort(arr);
// Take the max value from the array
int ans = arr[n - 1];
int maxPossible = arr[n - 1];
// Iterate from the end of the array
for (int i = n - 2; i >= 0; --i)
{
if (maxPossible > 0)
{
// Check for the number had
// come before or not
if (arr[i] >= maxPossible)
{
// Take the value which is
// not occured already
ans += (maxPossible - 1);
maxPossible = maxPossible - 1;
}
else
{
// Change max to new value
maxPossible = arr[i];
ans += maxPossible;
}
}
}
// Return the maximum sum
return ans;
}
// Driver Code
public static void Main(String[] args)
{
// Given array []arr
int []arr = { 4, 4, 1, 5 };
int n = arr.Length;
// Function Call
Console.Write(findMaxValue(arr, n));
}
}
// This code is contributed by sapnasingh4991
Javascript
13
时间复杂度: O(N)
辅助空间: O(1)