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++
// C++ program for the above approach#include <iostream>using namespace std;// Function to find maximum possible// sum after changing the array elements// as per the given constraintslong maxSum(int a[], int n){ // Stores the frequency of // elements in given array int count[n + 1] = {0}; // Update frequncy for(int i = 0; i < n; i++) count[min(a[i], n)]++; // Stores the previously // selected integer int size = 0; // Stores the maximum possible sum long ans = 0; // Traverse over array count[] for(int k = 1; k <= n; k++) { // Run loop for each k while (count[k] > 0 && size < k) { size++; ans += size; count[k]--; } // Update ans ans += k * count[k]; } // Return maximum possible sum return ans;}// Driver Codeint main(){ // Given array arr[] int arr[] = { 3, 5, 1 }; // Size of array int n = sizeof(arr) / sizeof(arr[0]); // Function Call cout << (maxSum(arr, n)); return 0;}// This code is contributed by akhilsaini |
Java
// Java program for the above approachimport java.util.*;class GFG { // Function to find maximum possible // sum after changing the array elements // as per the given constraints static long maxSum(int[] a) { // Length of given array int n = a.length; // Stores the frequency of // elements in given array int[] count = new int[n + 1]; // Update frequncy for (int x : a) count[Math.min(x, n)]++; // stores the previously // selected integer int size = 0; // Stores the maximum possible sum long ans = 0; // Traverse over array count[] for (int k = 1; k <= n; k++) { // Run loop for each k while (count[k] > 0 && size < k) { size++; ans += size; count[k]--; } // Update ans ans += k * count[k]; } // Return maximum possible sum return ans; } // Driver Code public static void main(String[] args) { // Given array arr[] int[] arr = { 3, 5, 1 }; // Function Call System.out.println(maxSum(arr)); }} |
Python3
# Python3 program for the above approach# Function to find maximum possible# sum after changing the array elements# as per the given constraintsdef maxSum(a, n): # Stores the frequency of # elements in given array count = [0] * (n + 1) # Update frequncy for i in range(0, n): count[min(a[i], n)] += 1 # stores the previously # selected integer size = 0 # Stores the maximum possible sum ans = 0 # Traverse over array count[] for k in range(1, n + 1): # Run loop for each k while (count[k] > 0 and size < k): size += 1 ans += size count[k] -= 1 # Update ans ans += k * count[k] # Return maximum possible sum return ans# Driver Codeif __name__ == '__main__': # Given array arr[] arr = [ 3, 5, 1 ] # Size of array n = len(arr) # Function Call print(maxSum(arr, n))# This code is contributed by akhilsaini |
C#
// C# program for the above approachusing System;class GFG{// Function to find maximum possible// sum after changing the array elements// as per the given constraintsstatic long maxSum(int[] a){ // Length of given array int n = a.Length; // Stores the frequency of // elements in given array int[] count = new int[n + 1]; // Update frequncy for(int i = 0; i < n; i++) count[Math.Min(a[i], n)]++; // stores the previously // selected integer int size = 0; // Stores the maximum possible sum long ans = 0; // Traverse over array count[] for(int k = 1; k <= n; k++) { // Run loop for each k while (count[k] > 0 && size < k) { size++; ans += size; count[k]--; } // Update ans ans += k * count[k]; } // Return maximum possible sum return ans;}// Driver Codepublic static void Main(){ // Given array arr[] int[] arr = { 3, 5, 1 }; // Function call Console.Write(maxSum(arr));}}// This code is contributed by akhilsaini |
6
Time Complexity: O(N), where N is the size of the given array.
Auxiliary Space: O(N)
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- Cost of rearranging the array such that no element exceeds the sum of its adjacent elements
- Maximize sum of atmost K elements in array by taking only corner elements | Set 2
- Min and max length subarray having adjacent element difference atmost K
- Maximize the maximum subarray sum after removing atmost one element
- Check whether an array can be fit into another array rearranging the elements in the array
- Longest subsequence such that absolute difference between every pair is atmost 1
- Arrange N elements in circular fashion such that all elements are strictly less than sum of adjacent elements
- Maximum sum after rearranging the array for K queries
- Length of longest common prefix possible by rearranging strings in a given array
- Minimum cost to remove the spaces between characters of a String by rearranging the characters
- Maximize array sum by replacing equal adjacent pairs by their sum and X respectively
- Maximise sum of absolute difference between adjacent elements in Array with sum K
- Minimum elements to be removed such that sum of adjacent elements is always odd
- Minimum elements to be removed such that sum of adjacent elements is always even
- Longest Subarray having sum of elements atmost 'k'
- Maximize removal of adjacent array elements based on their absolute value
- Maximum length prefix such that frequency of each character is atmost number of characters with minimum frequency
- Make palindromic string non-palindromic by rearranging its letters
- Largest number not greater than N which can become prime after rearranging its digits
- Count of distinct XORs formed by rearranging two Binary strings
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : akhilsaini

