Given an array arr[] consisting of N positive integers, the task is to maximize the sum of the array element such that the first element of the array is 1 and the difference between the adjacent elements of the array is at most 1 after performing the following operations:
- Rearrange the array elements in any way.
- Reduce any element to any number that is at least 1.
Examples:
Input: arr[] = {3, 5, 1}
Output: 6
Explanation:
One possible arrangement is {1, 2, 3} having maximum possible sum 6.
Input: arr[] = {1, 2, 2, 2, 3, 4, 5}
Output: 19
Explanation:
One possible arrangement is {1, 2, 2, 2, 3, 4, 5} having maximum possible sum 19.
Naive Approach: The simplest approach is to sort the given array then traverse in the sorted array and reduced the element that doesn’t satisfy the given condition.
Time Complexity: O(N * log N), where N is the size of the given array.
Auxiliary Space: O(N)
Efficient Approach: The idea is to use the Hashing concept of storing the frequencies of the elements of the given array. Follow the below steps to solve the problem:
- Create an auxiliary array count[] of size (N+1) to store frequency of arr[i].
- While storing the frequency in count[] and if arr[i] greater than N then increment count[N].
- Initialize the size and ans as 0 that stores the previously selected integer and maximum possible sum respectively.
- Traverse the given array count[] array using variable K and do the following:
- Iterate while a loop for each K until count[K] > 0 and size < K.
- Increment size by 1 and ans by size and reduce count[K] by 1 inside while loop.
- Increment ans with K*count[K] after the while loop ends.
- After the above steps, print the value of ans as the maximum possible sum.
Below is the implementation of the above approach:
C++
#include <iostream>
using namespace std;
long maxSum( int a[], int n)
{
int count[n + 1] = {0};
for ( int i = 0; i < n; i++)
count[min(a[i], n)]++;
int size = 0;
long ans = 0;
for ( int k = 1; k <= n; k++)
{
while (count[k] > 0 && size < k)
{
size++;
ans += size;
count[k]--;
}
ans += k * count[k];
}
return ans;
}
int main()
{
int arr[] = { 3, 5, 1 };
int n = sizeof (arr) / sizeof (arr[0]);
cout << (maxSum(arr, n));
return 0;
}
|
Java
import java.util.*;
class GFG {
static long maxSum( int [] a)
{
int n = a.length;
int [] count = new int [n + 1 ];
for ( int x : a)
count[(Math.min(x, n))]++;
int size = 0 ;
long ans = 0 ;
for ( int k = 1 ; k <= n; k++) {
while (count[k] > 0 && size < k) {
size++;
ans += size;
count[k]--;
}
ans += k * count[k];
}
return ans;
}
public static void main(String[] args)
{
int [] arr = { 3 , 5 , 1 };
System.out.println(maxSum(arr));
}
}
|
Python3
def maxSum(a, n):
count = [ 0 ] * (n + 1 )
for i in range ( 0 , n):
count[ min (a[i], n)] + = 1
size = 0
ans = 0
for k in range ( 1 , n + 1 ):
while (count[k] > 0 and size < k):
size + = 1
ans + = size
count[k] - = 1
ans + = k * count[k]
return ans
if __name__ = = '__main__' :
arr = [ 3 , 5 , 1 ]
n = len (arr)
print (maxSum(arr, n))
|
C#
using System;
class GFG{
static long maxSum( int [] a)
{
int n = a.Length;
int [] count = new int [n + 1];
for ( int i = 0; i < n; i++)
count[(Math.Min(a[i], n))]++;
int size = 0;
long ans = 0;
for ( int k = 1; k <= n; k++)
{
while (count[k] > 0 && size < k)
{
size++;
ans += size;
count[k]--;
}
ans += k * count[k];
}
return ans;
}
public static void Main()
{
int [] arr = { 3, 5, 1 };
Console.Write(maxSum(arr));
}
}
|
Javascript
<script>
function maxSum( a, n)
{
var count = Array(n+1).fill(0);
for ( var i = 0; i < n; i++)
count[(Math.min(a[i], n))]++;
var size = 0;
var ans = 0;
for ( var k = 1; k <= n; k++)
{
while (count[k] > 0 && size < k)
{
size++;
ans += size;
count[k]--;
}
ans += k * count[k];
}
return ans;
}
var arr = [ 3, 5, 1 ];
var n = arr.length;
document.write(maxSum(arr, n));
</script>
|
Time Complexity: O(N), where N is the size of the given array. As we are using a loop to traverse the array N times.
Auxiliary Space: O(N), as we are using a extra space for count array.