Arrange N elements in circular fashion such that all elements are strictly less than sum of adjacent elements
Last Updated :
13 Apr, 2023
Given an array of N integers, the task is to arrange them in a circular arrangement in such a way that the element is strictly less than the sum of its adjacent elements. In case such an arrangement is not possible, then print -1.
Note that there can be multiple ways of arranging the elements such that the condition is satisfied and the task is to find any such arrangement.
Examples:
Input: arr[] = {1, 4, 4, 3, 2}
Output: 1 3 4 4 2
arr[0] = 1 < (2 + 4)
arr[1] = 4 < (1 + 4)
arr[2] = 4 < (4 + 3)
arr[3] = 3 < (4 + 2)
arr[4] = 2 < (3 + 1)
Input: arr[] = {8, 13, 5}
Output: -1
Approach: The problem can be solved using a greedy approach, we first sort the array and then place the smallest element at the beginning, the second smallest at the end, the third smallest at the second position and the fourth smallest at the second last position in another array. Once the arrangement is completed, check if the given condition is satisfied or not.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void printArrangement( int a[], int n)
{
sort(a, a + n);
int b[n];
int low = 0, high = n - 1;
for ( int i = 0; i < n; i++) {
if (i % 2 == 0)
b[low++] = a[i];
else
b[high--] = a[i];
}
for ( int i = 0; i < n; i++) {
if (i == 0) {
if (b[n - 1] + b[1] <= b[i]) {
cout << -1;
return ;
}
}
else if (i == (n - 1)) {
if (b[n - 2] + b[0] <= b[i]) {
cout << -1;
return ;
}
}
else {
if (b[i - 1] + b[i + 1] <= b[i]) {
cout << -1;
return ;
}
}
}
for ( int i = 0; i < n; i++)
cout << b[i] << " " ;
}
int main()
{
int a[] = { 1, 4, 4, 3, 2 };
int n = sizeof (a) / sizeof (a[0]);
printArrangement(a, n);
return 0;
}
|
Java
import java.util.Arrays;
class GFG
{
static void printArrangement( int a[], int n)
{
Arrays.sort(a);
int b[] = new int [n];
int low = 0 , high = n - 1 ;
for ( int i = 0 ; i < n; i++)
{
if (i % 2 == 0 )
b[low++] = a[i];
else
b[high--] = a[i];
}
for ( int i = 0 ; i < n; i++)
{
if (i == 0 )
{
if (b[n - 1 ] + b[ 1 ] <= b[i])
{
System.out.print(- 1 );
return ;
}
}
else if (i == (n - 1 ))
{
if (b[n - 2 ] + b[ 0 ] <= b[i])
{
System.out.print(- 1 );
return ;
}
}
else
{
if (b[i - 1 ] + b[i + 1 ] <= b[i])
{
System.out.print(- 1 );
return ;
}
}
}
for ( int i = 0 ; i < n; i++)
System.out.print(b[i] + " " );
}
public static void main (String[] args)
{
int a[] = { 1 , 4 , 4 , 3 , 2 };
int n = a.length;
printArrangement(a, n);
}
}
|
Python3
def printArrangement(a, n):
a = sorted (a)
b = [ 0 for i in range (n)]
low = 0
high = n - 1
for i in range (n):
if (i % 2 = = 0 ):
b[low] = a[i]
low + = 1
else :
b[high] = a[i]
high - = 1
for i in range (n):
if (i = = 0 ):
if (b[n - 1 ] + b[ 1 ] < = b[i]):
print ( "-1" )
return
elif (i = = (n - 1 )) :
if (b[n - 2 ] + b[ 0 ] < = b[i]):
print ( "-1" )
return
else :
if (b[i - 1 ] + b[i + 1 ] < = b[i]):
print ( "-1" )
return
for i in range (n):
print (b[i], end = " " )
a = [ 1 , 4 , 4 , 3 , 2 ]
n = len (a)
printArrangement(a, n)
|
C#
using System;
class GFG
{
static void printArrangement( int []a, int n)
{
Array.Sort(a);
int []b = new int [n];
int low = 0, high = n - 1;
for ( int i = 0; i < n; i++)
{
if (i % 2 == 0)
b[low++] = a[i];
else
b[high--] = a[i];
}
for ( int i = 0; i < n; i++)
{
if (i == 0)
{
if (b[n - 1] + b[1] <= b[i])
{
Console.Write(-1);
return ;
}
}
else if (i == (n - 1))
{
if (b[n - 2] + b[0] <= b[i])
{
Console.Write(-1);
return ;
}
}
else
{
if (b[i - 1] + b[i + 1] <= b[i])
{
Console.Write(-1);
return ;
}
}
}
for ( int i = 0; i < n; i++)
Console.Write(b[i] + " " );
}
public static void Main ()
{
int []a = { 1, 4, 4, 3, 2 };
int n = a.Length;
printArrangement(a, n);
}
}
|
Javascript
<script>
function printArrangement(a, n)
{
a.sort();
var b = Array(n).fill(0);
var low = 0, high = n - 1;
for (i = 0; i < n; i++) {
if (i % 2 == 0)
b[low++] = a[i];
else
b[high--] = a[i];
}
for (i = 0; i < n; i++) {
if (i == 0) {
if (b[n - 1] + b[1] <= b[i]) {
document.write(-1);
return ;
}
}
else if (i == (n - 1)) {
if (b[n - 2] + b[0] <= b[i]) {
document.write(-1);
return ;
}
} else {
if (b[i - 1] + b[i + 1] <= b[i]) {
document.write(-1);
return ;
}
}
}
for (i = 0; i < n; i++)
document.write(b[i] + " " );
}
var a = [ 1, 4, 4, 3, 2 ];
var n = a.length;
printArrangement(a, n);
</script>
|
Time Complexity: O(N log N) //the inbuilt sort function takes N log N time to complete all operations, hence the overall time taken by the algorithm is N log N
Auxiliary Space: O(n) // an extra array is used to store all the elements and in worst case all elements will be stored hence algorithm takes up linear space
Share your thoughts in the comments
Please Login to comment...