给定大小为N的数组arr [] ,任务是通过遵循给定条件来找到数组的最大可能和:
- 在每一步骤中,只能使用一个元素来增加总和。
- 如果从数组中选择了某个元素K,则数组中的剩余数字将减少1。
- 数组中的元素不能减少到0以上。
例子:
Input: arr = {6, 6, 6}
Output: 15
Explanation:
Initially, the total sum is 0. Since all the elements are equal, any one element is chosen.
Sum after choosing the first six = 6. Remaining elements = {5, 5}.
Sum after choosing the five = 11. Remaining elements = {4}.
Finally, four is chosen making the maximum sum 15.
Input: arr = {0, 1, 0}
Output: 1
Explanation:
Initially, the total sum is 0. There is only one number that can be chosen in the array because rest of the elements are 0.
Therefore, the maximum sum = 1.
方法:由于所有其他元素的值都明显减少了1,因此,如果在每次迭代中选择最大元素,我们将获得最大和。因此,为了做到这一点,使用了分类。
- 想法是按降序对数组的元素进行排序。
- 现在,由于我们可以在每次迭代中选择最大值,因此我们可以在索引“ i”处将元素K的值计算为(K – i) 。
- 如果在任何索引处元素的值变为0,则该索引之后的所有元素将为0。
下面是上述方法的实现:
C++
// C++ program to find the maximum
// possible Sum for the given conditions
#include
using namespace std;
// Function to find the maximum
// possible sum for the
// given conditions
int maxProfit(int arr[], int n)
{
// Sorting the array
sort(arr, arr + n, greater());
// Variable to store the answer
int ans = 0;
// Iterating through the array
for(int i = 0; i < n; i++)
{
// If the value is greater than 0
if ((arr[i] - (1 * i)) > 0)
ans += (arr[i] - (1 * i));
// If the value becomes 0
// then break the loop because
// all the weights after this
// index will be 0
if ((arr[i] - (1 * i)) == 0)
break;
}
// Print profit
return ans;
}
// Driver code
int main()
{
int arr[] = {6, 6, 6};
int n = sizeof(arr) / sizeof(arr[0]);
cout << maxProfit(arr, n);
return 0;
}
// This code is contributed by ankitkumar34
Java
// Java program to find the maximum
// possible Sum for the given conditions
import java.util.Arrays;
import java.util.Collections;
public class GFG{
// Function to find the maximum
// possible sum for the
// given conditions
static int maxProfit(Integer [] arr)
{
// Sorting the array
Arrays.sort(arr, Collections.reverseOrder());
// Variable to store the answer
int ans = 0;
// Iterating through the array
for(int i = 0; i < arr.length; i++)
{
// If the value is greater than 0
if ((arr[i] - (1 * i)) > 0)
ans += (arr[i] - (1 * i));
// If the value becomes 0
// then break the loop because
// all the weights after this
// index will be 0
if ((arr[i] - (1 * i)) == 0)
break;
}
// Print profit
return ans;
}
// Driver code
public static void main(String[] args)
{
Integer arr[] = { 6, 6, 6 };
System.out.println(maxProfit(arr));
}
}
// This code is contributed by AnkitRai01
Python3
# Python3 program to find the maximum
# possible Sum for the given conditions
# Function to find the maximum
# possible sum for the
# given conditions
def maxProfit(arr):
# Sorting the array
arr.sort(reverse = True)
# Variable to store the answer
ans = 0
# Iterating through the array
for i in range(len(arr)):
# If the value is greater than 0
if (arr[i]-(1 * i))>0:
ans+=(arr[i]-(1 * i))
# If the value becomes 0
# then break the loop because
# all the weights after this
# index will be 0
if (arr[i]-(1 * i))== 0:
break
# print profit
return ans
# Driver code
if __name__ == "__main__":
arr = [6, 6, 6]
print(maxProfit(arr))
C#
// C# program to find the maximum
// possible Sum for the given conditions
using System;
class GFG{
// Function to find the maximum
// possible sum for the
// given conditions
static int maxProfit(int[] arr)
{
// Sorting the array
Array.Sort(arr);
Array.Reverse(arr);
// Variable to store the answer
int ans = 0;
// Iterating through the array
for(int i = 0; i < arr.Length; i++)
{
// If the value is greater than 0
if ((arr[i] - (1 * i)) > 0)
ans += (arr[i] - (1 * i));
// If the value becomes 0
// then break the loop because
// all the weights after this
// index will be 0
if ((arr[i] - (1 * i)) == 0)
break;
}
// Print profit
return ans;
}
// Driver code
static public void Main ()
{
int[] arr = { 6, 6, 6 };
Console.Write(maxProfit(arr));
}
}
// This code is contributed by Shubhamsingh10
输出:
15