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

📅  最后修改于: 2021-10-26 05:34:18             🧑  作者: 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)

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程