📌  相关文章
📜  通过根据给定条件选择元素,可能获得的最大修改数组总和

📅  最后修改于: 2021-05-17 04:32:53             🧑  作者: Mango

给定大小为N的数组arr [] ,任务是找到数组元素的最大可能和,以便可以根据以下条件选择元素:

  • 对于每个索引i ,元素的值为arr [i] ,那么我们可以将1到min(N,arr [i])的任何元素添加到总和中,否则保留该元素。
  • 如果某个元素已经添加到总和中,那么我们将无法再次将同一元素添加到该总和中。

例子:

方法:可以使用贪婪技术解决此问题。步骤如下:

  1. 以升序对数组的元素进行排序。
  2. 保持可以添加到总和的最大可能数(例如maxPossible)
  3. 用数组的最大元素初始化maxPossible并将其添加到总和中。
  4. 从头到尾遍历数组,然后检查给定元素值是否已添加到总和中。
  5. 如果之前已添加元素值,则将总和增加(element – 1) ,如果元素值小于maxPossible ,则将其添加并将maxPossible更改为元素值。
  6. 完成上述步骤后,打印总和的值。

下面是上述方法的实现:

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)