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

📅  最后修改于: 2021-09-02 06:31:21             🧑  作者: Mango

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

例子:

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

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

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

  • 按升序对数组 arr1[] 进行排序。
  • 按降序对数组 arr2[] 进行排序。
  • 迭代数组并检查元素 arr1[i] 和 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)

如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live