Maximum adjacent pair sum by array rearrangement
Last Updated :
11 Apr, 2024
Given an array arr[] of size N, find the maximum value of adjacent sum by rearranging array in any order.
Examples:
Input: arr[] = {1, 2, 3, 4}
Output: 17
Explanation: The arrangement is {1, 3, 4, 2} where sum of all the adjacent element is (1 + 3) + (3 + 4) + (4 + 2) = 17 which is maximum.
Input: arr[] = {5, 2, 3, 4, 6}
Output: 35
Explanation: The arrangement is {2, 5, 4, 6, 3} where sum of all adjacent element is (2 + 5) + (5 + 4) + (4 + 6) + (6 + 3) = 35 which is maximum.
Approach: To solve the problem, follow the below idea:
While calculating sum, each element comes 2 times except 2 elements(the first element and the last element) so to maximize sum we just keep the smallest 2 elements on the ends so that they don’t contribute to sum as much and since each elements comes 2 times(besides the end elements) so the final sum will be (2 * sum of all elements) – smallest element- second smallest element
Step-by-step algorithm:
- Find the smallest and the second smallest element in the array.
- Find the sum of all elements as sum.
- Return the maximum sum as (2 * sum) – smallest element – second smallest element.
Below is the implementation of the algorithm:
C++
#include <bits/stdc++.h>
using namespace std;
// Function to output max adj sum
void solve(int arr[], int N)
{
int smallestIdx = 0;
// traversing the array to find
// smallest element.
for (int i = 0; i < N; i++) {
if (arr[i] < arr[smallestIdx]) {
smallestIdx = i;
}
}
int secondSmallestIdx = 0;
if (smallestIdx == 0)
secondSmallestIdx = 1;
// traversing the array to find second smallest element
for (int i = 0; i < N; i++) {
if (arr[i] < arr[secondSmallestIdx]
&& smallestIdx != i) {
secondSmallestIdx = i;
}
}
// the maximum sum will be 2*sum of all the element
// minus the smallest and the second smallest element.
int sum = 0;
for (int i = 0; i < N; i++) {
sum += arr[i];
}
cout << "maximum adjacent sum is: "
<< 2 * sum - arr[smallestIdx]
- arr[secondSmallestIdx]
<< endl;
}
int main()
{
// Sample Input
int N = 5;
int arr[] = { 5, 2, 3, 4, 6 };
solve(arr, N);
return 0;
}
Java
import java.util.Arrays;
public class Main {
// Function to output max adjacent sum
static void solve(int[] arr, int N) {
int smallestIdx = 0;
// Traversing the array to find the smallest element
for (int i = 0; i < N; i++) {
if (arr[i] < arr[smallestIdx]) {
smallestIdx = i;
}
}
int secondSmallestIdx = 0;
if (smallestIdx == 0) {
secondSmallestIdx = 1;
}
// Traversing the array to find the second smallest element
for (int i = 0; i < N; i++) {
if (arr[i] < arr[secondSmallestIdx] && smallestIdx != i) {
secondSmallestIdx = i;
}
}
// The maximum sum will be 2 * sum of all the elements
// minus the smallest and the second smallest element.
int sum = Arrays.stream(arr).sum();
System.out.println("maximum adjacent sum is: " +
(2 * sum - arr[smallestIdx] - arr[secondSmallestIdx]));
}
public static void main(String[] args) {
// Sample Input
int N = 5;
int[] arr = {5, 2, 3, 4, 6};
solve(arr, N);
}
}
// This code is contributed by shivamgupta0987654321
Python3
# Function to output max adjacent sum
def solve(arr):
N = len(arr)
smallestIdx = 0
# Traversing the array to find the smallest element
for i in range(N):
if arr[i] < arr[smallestIdx]:
smallestIdx = i
secondSmallestIdx = 0
if smallestIdx == 0:
secondSmallestIdx = 1
# Traversing the array to find the second smallest element
for i in range(N):
if arr[i] < arr[secondSmallestIdx] and smallestIdx != i:
secondSmallestIdx = i
# The maximum sum will be 2 times the sum of all elements
# minus the smallest and the second smallest element.
sum_of_elements = sum(arr)
max_adj_sum = 2 * sum_of_elements - arr[smallestIdx] - arr[secondSmallestIdx]
print("Maximum adjacent sum is:", max_adj_sum)
arr = [5, 2, 3, 4, 6]
solve(arr)
JavaScript
function solve(arr) {
let N = arr.length;
let smallestIdx = 0;
// Traversing the array to find the smallest element
for (let i = 0; i < N; i++) {
if (arr[i] < arr[smallestIdx]) {
smallestIdx = i;
}
}
let secondSmallestIdx = 0;
if (smallestIdx === 0) {
secondSmallestIdx = 1;
}
// Traversing the array to find the second smallest element
for (let i = 0; i < N; i++) {
if (arr[i] < arr[secondSmallestIdx] && smallestIdx !== i) {
secondSmallestIdx = i;
}
}
// The maximum sum will be 2 times the sum of all elements
// minus the smallest and the second smallest element.
let sum_of_elements = arr.reduce((acc, curr) => acc + curr, 0);
let max_adj_sum = 2 * sum_of_elements - arr[smallestIdx] - arr[secondSmallestIdx];
console.log("Maximum adjacent sum is:", max_adj_sum);
}
let arr = [5, 2, 3, 4, 6];
solve(arr);
Outputmaximum adjacent sum is :35
Time Complexity: O(N), where N is the size of input array arr[].
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...