📌  相关文章
📜  重新排列两个给定的数组,以使相同索引元素的总和位于给定的范围内

📅  最后修改于: 2021-05-17 21:42:50             🧑  作者: Mango

给定两个数组arr1 []arr2 [],它们N个正整数和偶数K组成,任务是检查两个数组中相同索引元素的和是否在[K / 2,K]范围内是否重新排列给定的数组。如果有可能获得这样的安排,则打印“是” 。否则,打印“否”

例子:

天真的方法:最简单的方法来生成给定数组的所有可能排列,并检查是否有任何可能的排列满足给定条件。如果发现是真的,则打印“是” 。否则,打印“否”

时间复杂度: O((N!) 2 )
辅助空间: O(1)

高效方法:要优化上述方法,请按照以下步骤解决问题:

  • 以递增顺序对数组arr1 []进行排序。
  • 以降序对数组arr2 []进行排序。
  • 遍历数组,并检查元素irr [1]和arr2 [i]的和是否等于[0,N – 1]范围内i的所有可能值,是否位于[K / 2,K]范围内。如果发现是真的,则打印“是”。否则,打印“否”。

下面是上述方法的实现:

C++
// C++ program for the above approach
 
#include 
using namespace std;
 
// Function to check if there exists any
// arrangements of the arrays such that
// sum of element lie in the range [K/2, K]
void checkArrangement(int A1[], int A2[],
                      int n, int k)
{
    // Sort the array arr1[] in
    // increasing order
    sort(A1, A1 + n);
 
    // Sort the array arr2[] in
    // decreasing order
    sort(A2, A2 + n, greater());
 
    int flag = 0;
 
    // Traverse the array
    for (int i = 0; i < n; i++) {
 
        // If condition is not satisfied
        // break the loop
        if ((A1[i] + A2[i] > k)
            || (A1[i] + A2[i] < k / 2)) {
 
            flag = 1;
            break;
        }
    }
 
    // Print the result
    if (flag == 1)
        cout << "No";
    else
        cout << "Yes";
}
 
// Driver Code
int main()
{
    int arr1[] = { 1, 3, 4, 5 };
    int arr2[] = { 2, 0, 1, 1 };
 
    int K = 6;
 
    int N = sizeof(arr1)
 
            / sizeof(arr1[0]);
 
    checkArrangement(arr1, arr2, N, K);
 
    return 0;
}


Java
// Java program for the above approach
import java.io.*;
import java.util.*;
 
class GFG{
 
// Function to check if there exists any
// arrangements of the arrays such that
// sum of element lie in the range [K/2, K]
static void checkArrangement(Integer[] A1,
                             Integer[] A2,
                             int n, int k)
{
     
    // Sort the array arr1[] in
    // increasing order
    Arrays.sort(A1);
 
    // Sort the array arr2[] in
    // decreasing order
    Arrays.sort(A2, Collections.reverseOrder());
 
    int flag = 0;
 
    // Traverse the array
    for(int i = 0; i < n; i++)
    {
         
        // If condition is not satisfied
        // break the loop
        if ((A1[i] + A2[i] > k) ||
            (A1[i] + A2[i] < k / 2))
        {
            flag = 1;
            break;
        }
    }
 
    // Print the result
    if (flag == 1)
        System.out.println("No");
    else
        System.out.println("Yes");
}
 
// Driver Code
public static void main(String[] args)
{
    Integer[] arr1 = { 1, 3, 4, 5 };
    Integer[] arr2 = { 2, 0, 1, 1 };
 
    int K = 6;
 
    int N = arr1.length;
 
    checkArrangement(arr1, arr2, N, K);
}
}
 
// This code is contributed by akhilsaini


Python3
# Python3 program for the above approach
 
# Function to check if there exists any
# arrangements of the arrays such that
# sum of element lie in the range [K/2, K]
def checkArrangement(A1, A2, n, k):
     
    # Sort the array arr1[] in
    # increasing order
    A1 = sorted(A1)
 
    # Sort the array arr2[] in
    # decreasing order
    A2 = sorted(A2)
 
    A2 = A2[::-1]
 
    flag = 0
 
    # Traverse the array
    for i in range(n):
 
        # If condition is not satisfied
        # break the loop
        if ((A1[i] + A2[i] > k) or
            (A1[i] + A2[i] < k // 2)):
            flag = 1
            break
 
    # Print the result
    if (flag == 1):
        print("No")
    else:
        print("Yes")
 
# Driver Code
if __name__ == '__main__':
     
    arr1 = [ 1, 3, 4, 5 ]
    arr2 = [ 2, 0, 1, 1 ]
 
    K = 6
 
    N = len(arr1)
 
    checkArrangement(arr1, arr2, N, K)
 
# This code is contributed by mohit kumar 29


C#
// C# program for the above approach
using System;
using System.Collections;
 
class GFG{
 
// Function to check if there exists any
// arrangements of the arrays such that
// sum of element lie in the range [K/2, K]
static void checkArrangement(int[] A1, int[] A2,
                             int n, int k)
{
     
    // Sort the array arr1[] in
    // increasing order
    Array.Sort(A1);
 
    // Sort the array arr2[] in
    // decreasing order
    Array.Sort(A2);
    Array.Reverse(A2);
 
    int flag = 0;
 
    // Traverse the array
    for(int i = 0; i < n; i++)
    {
         
        // If condition is not satisfied
        // break the loop
        if ((A1[i] + A2[i] > k) ||
            (A1[i] + A2[i] < k / 2))
        {
            flag = 1;
            break;
        }
    }
 
    // Print the result
    if (flag == 1)
        Console.WriteLine("No");
    else
        Console.WriteLine("Yes");
}
 
// Driver Code
public static void Main()
{
    int[] arr1 = { 1, 3, 4, 5 };
    int[] arr2 = { 2, 0, 1, 1 };
 
    int K = 6;
 
    int N = arr1.Length;
 
    checkArrangement(arr1, arr2, N, K);
}
}
 
// This code is contributed by akhilsaini


Javascript


输出:
Yes

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