Cost of rearranging the array such that no element exceeds the sum of its adjacent elements
Last Updated :
16 Jun, 2022
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 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 arrangement 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
Approach: The problem can be solved using a Greedy Approach. The idea is to store 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++
#include <bits/stdc++.h>
using namespace std;
void Arrange( int arr[], int n)
{
int cost = 0;
unordered_map< int , int > index;
for ( int i = 0; i < n; i++) {
index[arr[i]] = i;
}
sort(arr, arr + n);
for ( int i = 0; i < n; i++) {
if (i == 0) {
if (arr[i] > arr[i + 1]
+ arr[n - 1]) {
cout << "-1" ;
return ;
}
else {
cost += abs (index[arr[i]] - i);
}
}
else if (i == n - 1) {
if (arr[i] > arr[i - 1]
+ arr[0]) {
cout << "-1" ;
return ;
}
else {
cost += abs (index[arr[i]] - i);
}
}
else {
if (arr[i] > arr[i - 1]
+ arr[i + 1]) {
cout << "-1" ;
return ;
}
else {
cost += abs (index[arr[i]] - i);
}
}
}
cout << cost;
return ;
}
int main()
{
int arr[] = { 2, 4, 5, 1, 3 };
int N = sizeof (arr) / sizeof (arr[0]);
Arrange(arr, N);
return 0;
}
|
Java
import java.util.*;
class GFG{
static void Arrange( int arr[], int n)
{
int cost = 0 ;
HashMap<Integer,
Integer> index = new HashMap<Integer,
Integer>();
for ( int i = 0 ; i < n; i++)
{
index.put(arr[i], i);
}
Arrays.sort(arr);
for ( int i = 0 ; i < n; i++)
{
if (i == 0 )
{
if (arr[i] > arr[i + 1 ] +
arr[n - 1 ])
{
System.out.print( "-1" );
return ;
}
else
{
cost += Math.abs(index.get(arr[i]) - i);
}
}
else if (i == n - 1 )
{
if (arr[i] > arr[i - 1 ] +
arr[ 0 ])
{
System.out.print( "-1" );
return ;
}
else
{
cost += Math.abs(index.get(arr[i]) - i);
}
}
else
{
if (arr[i] > arr[i - 1 ] +
arr[i + 1 ])
{
System.out.print( "-1" );
return ;
}
else
{
cost += Math.abs(index.get(arr[i]) - i);
}
}
}
System.out.print(cost);
return ;
}
public static void main(String[] args)
{
int arr[] = { 2 , 4 , 5 , 1 , 3 };
int N = arr.length;
Arrange(arr, N);
}
}
|
Python3
def Arrange(arr, n):
cost = 0
index = {}
for i in range (n):
index[arr[i]] = i
arr.sort()
for i in range (n):
if (i = = 0 ):
if (arr[i] > arr[i + 1 ] + arr[ - 1 ]):
print ( "-1" )
return
else :
cost + = abs (index[arr[i]] - i)
elif (i = = n - 1 ):
if (arr[i] > arr[i - 1 ] + arr[ 0 ]):
print ( "-1" )
return
else :
cost + = abs (index[arr[i]] - i)
else :
if (arr[i] > arr[i - 1 ] + arr[i + 1 ]):
print ( "-1" )
return
else :
cost + = abs (index[arr[i]] - i)
print (cost)
return
arr = [ 2 , 4 , 5 , 1 , 3 ]
N = len (arr)
Arrange(arr, N)
|
C#
using System;
using System.Collections.Generic;
class GFG{
static void Arrange( int []arr, int n)
{
int cost = 0;
Dictionary< int ,
int > index = new Dictionary< int ,
int >();
for ( int i = 0; i < n; i++)
{
index.Add(arr[i], i);
}
Array.Sort(arr);
for ( int i = 0; i < n; i++)
{
if (i == 0)
{
if (arr[i] > arr[i + 1] +
arr[n - 1])
{
Console.Write( "-1" );
return ;
}
else
{
cost += Math.Abs(index[arr[i]] - i);
}
}
else if (i == n - 1)
{
if (arr[i] > arr[i - 1] +
arr[0])
{
Console.Write( "-1" );
return ;
}
else
{
cost += Math.Abs(index[arr[i]] - i);
}
}
else
{
if (arr[i] > arr[i - 1] +
arr[i + 1])
{
Console.Write( "-1" );
return ;
}
else
{
cost += Math.Abs(index[arr[i]] - i);
}
}
}
Console.Write(cost);
return ;
}
public static void Main(String[] args)
{
int []arr = { 2, 4, 5, 1, 3 };
int N = arr.Length;
Arrange(arr, N);
}
}
|
Javascript
<script>
function Arrange(arr, n)
{
let cost = 0;
let index = new Map();
for (let i = 0; i < n; i++) {
index.set(arr[i], i)
}
arr.sort();
for (let i = 0; i < n; i++) {
if (i == 0) {
if (arr[i] > arr[i + 1] + arr[n - 1]) {
document.write( "-1" );
return ;
}
else {
cost += Math.abs(index.get(arr[i]) - i);
}
}
else if (i == n - 1) {
if (arr[i] > arr[i - 1] + arr[0]) {
document.write( "-1" );
return ;
}
else {
cost += Math.abs(index.get(arr[i]) - i);
}
}
else {
if (arr[i] > arr[i - 1] + arr[i + 1]) {
document.write( "-1" );
return ;
}
else {
cost += Math.abs(index.get(arr[i]) - i);
}
}
}
document.write(cost);
return ;
}
let arr = [ 2, 4, 5, 1, 3 ];
let N = arr.length;
Arrange(arr, N);
</script>
|
Output:
10
Time Complexity: O(N log N)
Auxiliary Space: O(N)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...