Given an array arr[] of N unique integers, the task is to find the cost to arrange them in a circular arrangement in such a way that every element is less than or equal to the the sum of its adjacent elements.

Cost of moving an element from index i in original array to index j in final arrangement is **|i – j|**

In case such an arrangement is not possible, then print -1.

Examples:

Input:_ arr[] = {2, 4, 5, 1, 3} _

Output:_ 10 _

Explanation:

_One of the possible arrangment is {1, 2, 3, 4, 5} _

_For index 1, 1 ≤ 4 + 2, cost = 4 – 1 = 3 _

_For index 2, 2 ≤ 1 + 3, cost = 3 + (2 – 1) = 4 _

_For index 3, 3 ≤ 2 + 4, cost = 4 + (5 – 3) = 6 _

_For index 4, 4 ≤ 3 + 4, cost = 6 + (4 – 2) = 8 _

For index 5, 5 ≤ 4 + 1, cost = 8 + (5 – 3) = 10

Input:_ arr[] = {1, 10, 100, 1000} _

Output:_ -1_

Recommended: Please try your approach on {IDE} first, before moving on to the solution.

Approach: The problem can be solved using a Greedy Approach. The idea is to store the the original index of the elements in a hashmap and then sort the array. Now check if the given condition is satisfied or not. If found to be true, then calculate the cost by adding up the difference between the current and previous indices. Otherwise, print -1.

Below is the implementation of the above approach:

  • C++
  • Java
  • Python3

// C++ program to implement

// the above approach

#include <bits/stdc++.h>

**using** **namespace** std;

// Function to check if given elements

// can be arranged such that sum of

// its neighbours is strictly greater

**void** Arrange(``**int** arr[], **int** n)

{

// Initialize the total cost

**int** cost = 0;

// Storing the orginal index of

// elements in a hashmap

unordered_map<``**int**``, **int**``> index;

**for** (``**int** i = 0; i < n; i++) {

index[arr[i]] = i;

}

// Sort the given array

sort(arr, arr + n);

// Check if a given condition

// is satisfies or not

**for** (``**int** i = 0; i < n; i++) {

// First number

**if** (i == 0) {

**if** (arr[i] > arr[i + 1]

+ arr[n - 1]) {

cout << "-1"``;

**return**``;

}

**else** {

// Add the cost to overall cost

cost += **abs**``(index[arr[i]] - i);

}

}

// Last number

**else** **if** (i == n - 1) {

**if** (arr[i] > arr[i - 1]

+ arr[0]) {

cout << "-1"``;

**return**``;

}

**else** {

// Add the cost to

// overall cost

cost += **abs**``(index[arr[i]] - i);

}

}

**else** {

**if** (arr[i] > arr[i - 1]

+ arr[i + 1]) {

cout << "-1"``;

**return**``;

}

**else** {

// Add the cost to

// overall cost

cost += **abs**``(index[arr[i]] - i);

}

}

}

// Printing the cost

cout << cost;

**return**``;

}

// Driver Code

**int** main()

{

// Given array

**int** arr[] = { 2, 4, 5, 1, 3 };

**int** N = **sizeof**``(arr) / **sizeof**``(arr[0]);

// Function call

Arrange(arr, N);

**return** 0;

}

Output:

10

Time Complexity:_ O(N log N) _

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.

#arrays #greedy #mathematical #sorting #array-rearrange #cpp-unordered_map

Cost of rearranging the array such that no element exceeds the sum
1.45 GEEK