📌  相关文章
📜  以圆形方式排列N个元素,以使所有元素严格小于相邻元素的总和

📅  最后修改于: 2021-04-29 02:49:53             🧑  作者: Mango

给定一个由N个整数组成的数组,任务是将它们以圆形排列方式进行布置,以使该元素严格小于其相邻元素的总和。如果这样的安排是不可能的,则打印-1
注意,可以有多种方式来布置元素,从而满足条件,并且任务是找到任何这样的布置。
例子:

方法:可以使用贪婪方法解决问题,我们首先对数组进行排序,然后将最小的元素放在开头,第二个最小放在结尾,第三个最小放在第二个位置,第四个放在倒数第二个位置在另一个数组中。安排完成后,检查是否满足给定条件。
下面是上述方法的实现:

C++
// C++ implementation of the approach
#include 
using namespace std;
 
// Function to print the arrangement that
// satisifes the given condition
void printArrangement(int a[], int n)
{
 
    // Sort the array initially
    sort(a, a + n);
 
    // Array that stores the arrangement
    int b[n];
 
    // Once the array is sorted
    // Re-fill the array again in the
    // mentioned way in the approach
    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];
    }
 
    // Iterate in the array
    // and check if the arrangement made
    // satisfies the given condition or not
    for (int i = 0; i < n; i++) {
 
        // For the first element
        // the adjacents will be a[1] and a[n-1]
        if (i == 0) {
            if (b[n - 1] + b[1] <= b[i]) {
                cout << -1;
                return;
            }
        }
 
        // For the last element
        // the adjacents will be a[0] and a[n-2]
        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;
            }
        }
    }
 
    // If we reach this position then
    // the arrangement is possible
    for (int i = 0; i < n; i++)
        cout << b[i] << " ";
}
 
// Driver code
int main()
{
    int a[] = { 1, 4, 4, 3, 2 };
    int n = sizeof(a) / sizeof(a[0]);
 
    printArrangement(a, n);
 
    return 0;
}


Java
// Java implementation of the approach
import java.util.Arrays;
 
class GFG
{
 
// Function to print the arrangement that
// satisifes the given condition
static void printArrangement(int a[], int n)
{
 
    // Sort the array initially
    Arrays.sort(a);
 
    // Array that stores the arrangement
    int b[] = new int[n];
 
    // Once the array is sorted
    // Re-fill the array again in the
    // mentioned way in the approach
    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];
    }
 
    // Iterate in the array
    // and check if the arrangement made
    // satisfies the given condition or not
    for (int i = 0; i < n; i++)
    {
 
        // For the first element
        // the adjacents will be a[1] and a[n-1]
        if (i == 0)
        {
            if (b[n - 1] + b[1] <= b[i])
            {
                System.out.print(-1);
                return;
            }
        }
 
        // For the last element
        // the adjacents will be a[0] and a[n-2]
        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;
            }
        }
    }
 
    // If we reach this position then
    // the arrangement is possible
    for (int i = 0; i < n; i++)
        System.out.print(b[i] + " ");
}
 
// Driver code
public static void main (String[] args)
{
    int a[] = { 1, 4, 4, 3, 2 };
    int n = a.length;
 
    printArrangement(a, n);
}
}
 
// This code is contributed by anuj_67..


Python3
# Python3 implementation of the approach
 
# Function to print the arrangement that
# satisifes the given condition
def printArrangement(a, n):
 
    # Sort the array initially
    a = sorted(a)
 
    # Array that stores the arrangement
    b = [0 for i in range(n)]
 
    # Once the array is sorted
    # Re-fill the array again in the
    # mentioned way in the approach
    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
 
    # Iterate in the array
    # and check if the arrangement made
    # satisfies the given condition or not
    for i in range(n):
 
        # For the first element
        # the adjacents will be a[1] and a[n-1]
        if (i == 0):
            if (b[n - 1] + b[1] <= b[i]):
                print("-1")
                return
                 
        # For the last element
        # the adjacents will be a[0] and a[n-2]
        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
 
    # If we reach this position then
    # the arrangement is possible
    for i in range(n):
        print(b[i], end = " ")
 
# Driver code
a = [ 1, 4, 4, 3, 2 ]
n = len(a)
 
printArrangement(a, n)
 
# This code is contributed by Mohit Kumar


C#
// C# implementation of the approach
using System;
 
class GFG
{
 
// Function to print the arrangement that
// satisifes the given condition
static void printArrangement(int []a, int n)
{
 
    // Sort the array initially
    Array.Sort(a);
 
    // Array that stores the arrangement
    int []b = new int[n];
 
    // Once the array is sorted
    // Re-fill the array again in the
    // mentioned way in the approach
    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];
    }
 
    // Iterate in the array
    // and check if the arrangement made
    // satisfies the given condition or not
    for (int i = 0; i < n; i++)
    {
 
        // For the first element
        // the adjacents will be a[1] and a[n-1]
        if (i == 0)
        {
            if (b[n - 1] + b[1] <= b[i])
            {
                Console.Write(-1);
                return;
            }
        }
 
        // For the last element
        // the adjacents will be a[0] and a[n-2]
        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;
            }
        }
    }
 
    // If we reach this position then
    // the arrangement is possible
    for (int i = 0; i < n; i++)
        Console.Write(b[i] + " ");
}
 
// Driver code
public static void Main ()
{
    int []a = { 1, 4, 4, 3, 2 };
    int n = a.Length;
 
    printArrangement(a, n);
}
}
 
// This code is contributed by anuj_67..


Javascript


输出:
1 3 4 4 2

时间复杂度: O(N log N)
辅助空间: O(n)