Maximize sum of given array by rearranging array such that the difference between adjacent elements is atmost 1
Last Updated :
24 Mar, 2023
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 constraints
long maxSum(int a[], int n)
{
// Stores the frequency of
// elements in given array
int count[n + 1] = {0};
// Update frequency
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 Code
int 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 approach
import 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 frequency
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 constraints
def maxSum(a, n):
# Stores the frequency of
# elements in given array
count = [0] * (n + 1)
# Update frequency
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 Code
if __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 approach
using System;
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 frequency
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 Code
public static void Main()
{
// Given array arr[]
int[] arr = { 3, 5, 1 };
// Function call
Console.Write(maxSum(arr));
}
}
// This code is contributed by akhilsaini
JavaScript
<script>
// Javascript program for the above approach
// Function to find maximum possible
// sum after changing the array elements
// as per the given constraints
function maxSum( a, n)
{
// Stores the frequency of
// elements in given array
var count = Array(n+1).fill(0);
// Update frequency
for(var i = 0; i < n; i++)
count[(Math.min(a[i], n))]++;
// Stores the previously
// selected integer
var size = 0;
// Stores the maximum possible sum
var ans = 0;
// Traverse over array count[]
for(var 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
// Given array arr[]
var arr = [ 3, 5, 1 ];
// Size of array
var n = arr.length;
// Function Call
document.write(maxSum(arr, n));
// This code is contributed by noob2000.
</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.
Similar Reads
Maximize sum of absolute difference between adjacent elements in Array with sum K Given two integers N and K, the task is to maximize the sum of absolute differences between adjacent elements of an array of length N and sum K. Examples: Input: N = 5, K = 10 Output: 20 Explanation: The array arr[] with sum 10 can be {0, 5, 0, 5, 0}, maximizing the sum of absolute difference of adj
4 min read
Maximize element at index K in an array with at most sum M and difference between adjacent elements at most 1 Given a positive integer N, the task is to construct an array of length N and find the maximum value at index K such that the sum of all the array elements is at most M and the absolute difference between any two consecutive array elements is at most 1.Examples:Input: N = 3, M = 7, K = 1Output: 3Exp
6 min read
Rearrange array such that difference of adjacent elements is in descending order Given an array a[] with n integers the task is to rearrange the elements of the array in such a way that the differences of the adjacent elements are in descending order.Examples: Input : arr[] = {1, 2, 3, 4, 5, 6} Output : 6 1 5 2 4 3 Explanation: For first two elements the difference is abs(6-1)=5
6 min read
Maximize score by rearranging Array such that absolute difference of first and last element is minimum Given an array arr[] of size N, the task is to rearrange the array to achieve the maximum possible score, keeping the absolute difference of the first and last element as minimum as possible. The distribution of score is given as: If arr[i] < arr[i+1], increment score by 1.Else keep the score sam
9 min read
Find the Longest subarray such that difference between adjacent elements is K Given an array arr[] of size N, and integer K. The task is to find the longest subarray with the difference between adjacent elements as K. Examples: Input: arr[] = { 5, 5, 5, 10, 8, 6, 12, 13 }, K =1Output: {12, 13}Explanation: This is the longest subarray with difference between adjacents as 1. In
6 min read
Smallest number that can replace all -1s in an array such that maximum absolute difference between any pair of adjacent elements is minimum Given an array arr[] consisting of N positive integers and some elements as -1, the task is to find the smallest number, say K, such that replacing all -1s in the array by K minimizes the maximum absolute difference between any pair of adjacent elements. Examples: Input: arr[] = {-1, 10, -1, 12, -1}
8 min read