Search code examples
c++arraysalgorithmsortingdata-structures

Maximizing the sum of Adjacent sum in an array


i am getting wrong answer for some test cases for a problem mentioned below and i have explained my approach further. Please help me identify the flaw with my logic.

Problem Statement: You are given an array A of size (N≥2). F(A) = Sum of(A(i) + A(i+1)); For eg: for array A[1,2,3] , f(a) = (1+2)+(2+3) Find the maximum value of f(A) you can obtain by rearranging the elements of A in any arbitrary order. Link for the problem:

#include <iostream>
#include <algorithm> 
#include <vector>    

using namespace std;

bool compare(int a, int b) {
    return a > b;
}

int calculateFunction(vector<int>& arr) {
    int sum = 0;
    for (int i = 0; i < arr.size() - 1; i++) {
        sum += arr[i] + arr[i + 1];
    }
    return sum;
}

int main() {
    int t;
    cin >> t;
    while (t--) {
        int n;
        cin >> n;
        vector<int> arr(n);
        for (int j = 0; j < n; j++) {
            cin >> arr[j];
        }
        sort(arr.begin(), arr.end());
        vector<int> sol(n);
        int x = 0;
        for (int j = 0; j < n; j += 2) {
            sol[x++] = arr[j];
        }
        sort(arr.begin(), arr.end(), compare);
        for (int j = 0; j < n; j += 2) {
            sol[x++] = arr[j];
        }
        int sum = calculateFunction(sol);
        cout << sum << endl;
    }
    return 0;
}

I firstly did hit and trial approach on paper for an array [1,2,3,4,5,6] and saw the maximum adjacent sum to be [1,3,5,6,4,2] (Baiscally max values in between with descending values at left and right). For that, I first arranged the array in ascending order [1,2,3,4,5,6], stored its alternative values in an array [1,3,5], then arrange it in descending order [6,5,4,3,2,1] and appended alternative values to the array that made it [1,3,5,6,4,2]. Then printed out the function for summation as expected. This sol worked for test cases [3,6] & [2,2,1,2,2]


Solution

  • You are given an array A of size N (N≥2).

    We define i=1, ..., N−1 f(A)=∑(Ai+Ai+1).

    Find the maximum value of f(A) you can obtain by rearranging the elements of A in any arbitrary order.

    So, whatever the order, the end sum is:

    (A1 + A2) + (A2 + A3) ... + (An-2 + An-1) + (An-1 + An)

    which is:

    A1 + 2(A2 + A3 + ... + An-1) + An

    Hence, given the fact that + is commutative and associative, you can maximize this sum by finding the two smallest elements and swap one of them with the first and the other of them with the second. So:

    int min, min2, minIndex = 0, minIndex2 = 1;
    if (arr[0] < arr[1]) {
        min = arr[0];
        min2 = arr[1];
    } else {
        min = arr[1];
        min2 = arr[0];
    }
    for (int i = 2; i < size; i++) {
        if (arr[i] <= min) {
            min2 = min;
            min = arr[i];
            min2Index = minIndex;
            minIndex = i;
        } else if (arr[i] < min2) {
            min2Index = i;
            min2 = arr[i];
        }
    }
    int swap = arr[0];
    arr[0] = min;
    arr[minIndex] = swap;
    swap = arr[size - 1];
    arr[size - 1] = min2;
    arr[minIndex2] = swap;
    

    The solution above assumes you have at least 2 elements, so you can implement a function that starts with validating the input and handling trivial cases and then continuing with the continuation above.