📌  相关文章
📜  重新排列数组,使相同索引元素的总和最多为 K

📅  最后修改于: 2021-09-07 02:07:20             🧑  作者: Mango

给定两个数组A[]B[] ,每个数组由N 个整数和一个整数K 组成,任务是重新排列数组B[]使得A i + B i 的和最多为K 。如果没有这样的安排是可能的,打印-1

例子:

方法:数组B[]满足给定条件的最佳重新排列是按降序对数组进行排序。

证明:

请按照以下步骤解决问题:

  • 按降序对数组B[]进行排序。
  • 遍历数组并检查每个i索引的A i + B i是否小于或等于K。
  • 如果任何对的条件失败,则打印-1
  • 否则,打印数组B[]

下面是上述方法的实现:

C++
// C++ program for the above approach
 
#include 
using namespace std;
 
// Function to rearrange array such
// that sum of similar indexed elements
// does not exceed K
void rearrangeArray(int A[], int B[],
                    int N, int K)
{
    // Sort the array B[]
    // in descending order
    sort(B, B + N, greater());
 
    bool flag = true;
 
    for (int i = 0; i < N; i++) {
 
        // If conditon fails
        if (A[i] + B[i] > K) {
            flag = false;
            break;
        }
    }
 
    if (!flag) {
 
        cout << "-1" << endl;
    }
    else {
 
        // Print the array
        for (int i = 0; i < N; i++) {
            cout << B[i] << " ";
        }
    }
}
 
// Driver Code
int main()
{
    // Given arrays
    int A[] = { 1, 2, 3, 4, 2 };
    int B[] = { 1, 2, 3, 1, 1 };
 
    int N = sizeof(A) / sizeof(A[0]);
 
    int K = 5;
 
    rearrangeArray(A, B, N, K);
 
    return 0;
}


Java
// Java program for the above approach
import java.util.*;
 
class GFG{
 
// Reverse array
static int[] reverse(int a[])
{
    int i, n = a.length, t;
    for(i = 0; i < n / 2; i++)
    {
        t = a[i];
        a[i] = a[n - i - 1];
        a[n - i - 1] = t;
    }
    return a;
}
     
// Function to rearrange array such
// that sum of similar indexed elements
// does not exceed K
static void rearrangeArray(int A[], int B[],
                           int N, int K)
{
     
    // Sort the array B[]
    // in descending order
    Arrays.sort(B);
    B = reverse(B);
 
    boolean flag = true;
 
    for(int i = 0; i < N; i++)
    {
         
        // If conditon fails
        if (A[i] + B[i] > K)
        {
            flag = false;
            break;
        }
    }
 
    if (!flag)
    {
        System.out.print("-1" + "\n");
    }
    else
    {
         
        // Print the array
        for(int i = 0; i < N; i++)
        {
            System.out.print(B[i] + " ");
        }
    }
}
 
// Driver Code
public static void main(String[] args)
{
     
    // Given arrays
    int A[] = { 1, 2, 3, 4, 2 };
    int B[] = { 1, 2, 3, 1, 1 };
 
    int N = A.length;
 
    int K = 5;
 
    rearrangeArray(A, B, N, K);
}
}
 
// This code is contributed by Amit Katiyar


Python3
# Python3 program for the above approach
 
# Function to rearrange array such
# that sum of similar indexed elements
# does not exceed K
def rearrangeArray(A, B, N, K):
     
    # Sort the array B[]
    # in descending order
    B.sort(reverse = True)
 
    flag = True
 
    for i in range(N):
         
        # If conditon fails
        if (A[i] + B[i] > K):
            flag = False
            break
 
    if (flag == False):
        print("-1")
    else:
         
        # Print the array
        for i in range(N):
            print(B[i], end = " ")
 
# Driver Code
if __name__ == '__main__':
     
    # Given arrays
    A = [ 1, 2, 3, 4, 2 ]
    B = [ 1, 2, 3, 1, 1 ]
 
    N = len(A)
    K = 5;
 
    rearrangeArray(A, B, N, K)
 
# This code is contributed by SURENDRA_GANGWAR


C#
// C# program for the
// above approach
using System;
class GFG{
 
// Reverse array
static int[] reverse(int []a)
{
  int i, n = a.Length, t;
   
  for(i = 0; i < n / 2; i++)
  {
    t = a[i];
    a[i] = a[n - i - 1];
    a[n - i - 1] = t;
  }
  return a;
}
     
// Function to rearrange array such
// that sum of similar indexed elements
// does not exceed K
static void rearrangeArray(int []A, int []B,
                           int N, int K)
{   
  // Sort the array []B
  // in descending order
  Array.Sort(B);
  B = reverse(B);
 
  bool flag = true;
 
  for(int i = 0; i < N; i++)
  {
    // If conditon fails
    if (A[i] + B[i] > K)
    {
      flag = false;
      break;
    }
  }
 
  if (!flag)
  {
    Console.Write("-1" + "\n");
  }
  else
  {
    // Print the array
    for(int i = 0; i < N; i++)
    {
      Console.Write(B[i] + " ");
    }
  }
}
 
// Driver Code
public static void Main(String[] args)
{   
  // Given arrays
  int []A = {1, 2, 3, 4, 2};
  int []B = {1, 2, 3, 1, 1};
 
  int N = A.Length;
  int K = 5;
  rearrangeArray(A, B, N, K);
}
}
 
// This code is contributed by Rajput-Ji


Javascript


输出:
3 2 1 1 1

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

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