Find the Maximum possible Sum for the given conditions
Last Updated :
28 Jun, 2022
Given an array arr[] of size N, the task is to find the maximum possible sum of the array by following the given conditions:
- At every step, only one element can be used to increase the sum.
- If some element K is selected from the array, the remaining numbers in the array get reduced by one.
- The elements in the array can't be reduced past 0.
Examples:
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.
Approach: Since the value of all the other elements gets reduced by 1, clearly, we get the maximum sum if we choose the maximum element at every iteration. Therefore, in order to do this, sorting is used.
- The idea is to sort the elements of the array in descending order.
- Now, since we get to choose the maximum value at every iteration, we calculate the value of the element K at some index 'i' as (K - i).
- If at any index the value of the element becomes 0, then all the elements past that index will be 0.
Below is the implementation of the above approach:
C++
// C++ program to find the maximum
// possible Sum for the given conditions
#include<bits/stdc++.h>
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<int>());
// 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
JavaScript
<script>
// Javascript program to find the maximum
// possible Sum for the given conditions
// Function to find the maximum
// possible sum for the
// given conditions
function maxProfit(arr)
{
// Sorting the array
arr.sort();
arr.reverse();
// Variable to store the answer
let ans = 0;
// Iterating through the array
for(let 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
let arr = [ 6, 6, 6 ];
document.write(maxProfit(arr));
// This code is contributed by divyesh072019
</script>
Time Complexity: O(n*log(n))
Auxiliary Space: O(1)
Similar Reads
Given count of digits 1, 2, 3, 4, find the maximum sum possible Given the count of digits 1, 2, 3, 4. Using these digits you are allowed to only form numbers 234 and 12. The task is to find the maximum possible sum that can be obtained after forming the numbers. Note: The aim is only to maximize the sum, even if some of the digits left unused. Examples: Input :
5 min read
Find the maximum sum (a+b) for a given input integer N satisfying the given condition Given an integer N, the task is to find the largest sum (a + b) {1 ? a ? N, 1 ? b ? N} such that a * b/(a + b) is an integer (i.e a + b divides a * b) and a != b. Examples: Input: N = 10 Output: 9 Explanation: The numbers a = 3 and b = 6 leads to sum = 9 also 6 * 3 = 18 which is divisible by 6 + 3 =
11 min read
Maximum sum combination from the given array Given an array arr[] of N integers and three integers X, Y and Z. The task is to find the maximum value of (arr[i] * X) + (arr[j] * Y) + (arr[k] * Z) where 0 ? i ? j ? k ? N - 1. Examples: Input: arr[] = {1, 5, -3, 4, -2}, X = 2, Y = 1, Z = -1 Output: 18 (2 * 5) + (1 * 5) + (-1 * -3) = 18 is the max
12 min read
Find the Maximum sum of the Array by performing the given operations Given an Array A[] of size N with repeated elements and all array elements are positive, the task is to find the maximum sum by applying the given operations: Select any 2 indexes and select 2 integers(say x and y) such that the product of the elements(x and y) is equal to the product of the element
5 min read
Find out the maximum value of the expression according to the given constraint Given an array A[] of length N. Then your task is to output the maximum value of (Sum/LCM). Where Sum is the addition of all the elements of A[] and LCM is a number between [1, â] that can't be achieved by any possible non-empty set of A[]. Examples: Input: N = 5, A[] = {1, 2, 3, 4, 5} Output: 2 Exp
9 min read